@@ -103,21 +103,14 @@ public function updateWithUserKey(
103103 );
104104
105105 $ this ->userApi ->mergeUsers ($ apiAwareResource , $ userByEmailFromForm ['id ' ], [$ userFoundByKey ['id ' ]]);
106- }
107106
108- if (!isset ($ user ) || false === array_key_exists (AbstractClient::ERROR , $ user )) {
109- $ user = $ this ->userApi ->createUser ($ apiAwareResource , $ payload );
110- }
107+ $ this ->changeCookieWithEvent ($ user , $ apiAwareResource , $ eventName );
111108
112- if (false === is_array ($ user ) ||
113- false === array_key_exists ('id ' , $ user ) ||
114- false === array_key_exists ('user_key ' , $ user )
115- ) {
116- throw new \RuntimeException ('User might not be created or updated properly. ' );
109+ return $ user ;
117110 }
118111
119- $ this ->cookieManager -> setUserComCookie ( $ user [ ' user_key ' ] );
120- $ this ->sendEvent ( $ apiAwareResource , $ user [ ' email ' ] , $ eventName );
112+ $ user = $ this ->userApi -> createUser ( $ apiAwareResource , $ payload );
113+ $ this ->changeCookieWithEvent ( $ user , $ apiAwareResource , $ eventName );
121114
122115 return $ user ;
123116 }
@@ -136,22 +129,44 @@ private function updateForUserWithoutEmail(
136129 UserApiInterface::EMAIL_PROPERTY ,
137130 );
138131
139- $ id = null !== $ customerFoundByEmail ?
140- $ customerFoundByEmail ['id ' ] :
141- $ userFromUserKey ['id ' ];
132+ $ customerFoundByEmailId = null !== $ customerFoundByEmail && isset ($ customerFoundByEmail ['id ' ])
133+ ? $ customerFoundByEmail ['id ' ]
134+ : null
135+ ;
136+
137+ $ id = $ customerFoundByEmailId ?? $ userFromUserKey ['id ' ];
142138
143139 $ user = $ this ->userApi ->updateUser (
144140 $ apiAwareResource ,
145141 $ id ,
146142 $ this ->buildPayload ($ email , $ customer , $ address ),
147143 );
148144
149- if (null !== $ customerFoundByEmail ) {
150- $ this ->userApi ->mergeUsers ($ apiAwareResource , $ id , [$ userFromUserKey ]);
145+ if (is_array ($ customerFoundByEmail ) &&
146+ array_key_exists ('id ' , $ customerFoundByEmail ) &&
147+ array_key_exists ('id ' , $ userFromUserKey )
148+ ) {
149+ $ this ->userApi ->mergeUsers ($ apiAwareResource , $ customerFoundByEmail ['id ' ], [$ userFromUserKey ['id ' ]]);
151150 }
152151
153152 $ this ->sendEvent ($ apiAwareResource , $ email , $ eventName );
154153
155154 return $ user ;
156155 }
156+
157+ public function changeCookieWithEvent (
158+ ?array $ user ,
159+ UserComApiAwareInterface $ apiAwareResource ,
160+ string $ eventName ,
161+ ): void {
162+ if (false === is_array ($ user ) ||
163+ false === array_key_exists ('id ' , $ user ) ||
164+ false === array_key_exists ('user_key ' , $ user )
165+ ) {
166+ throw new \RuntimeException ('User might not be created or updated properly. ' );
167+ }
168+
169+ $ this ->cookieManager ->setUserComCookie ($ user ['user_key ' ]);
170+ $ this ->sendEvent ($ apiAwareResource , $ user ['email ' ], $ eventName );
171+ }
157172}
0 commit comments