Skip to content

Commit cbec8a9

Browse files
committed
Refactor user update flow & adjust name of product while creating deal
1 parent 767f840 commit cbec8a9

4 files changed

Lines changed: 63 additions & 18 deletions

File tree

src/Api/AbstractClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ protected function request(
4949

5050
return $this->request($path, $method, $options, true);
5151
}
52-
5352
if ($status >= Response::HTTP_OK && $status < Response::HTTP_MULTIPLE_CHOICES) {
53+
$this->logger->debug(sprintf(
54+
'200 User.com API request',
55+
), [
56+
'path' => $path,
57+
'method' => $method,
58+
'options' => $options,
59+
'response' => $response->getContent(false),
60+
]);
61+
5462
return $response->toArray();
5563
}
5664

src/Handler/OrderStateUpdateHandler.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use BitBag\SyliusUserComPlugin\Trait\UserComApiAwareInterface;
2020
use Psr\Log\LoggerInterface;
2121
use Sylius\Component\Core\Model\OrderInterface;
22+
use Sylius\Component\Product\Model\ProductInterface;
23+
use Sylius\Component\Product\Model\ProductVariantInterface;
2224
use Webmozart\Assert\Assert;
2325

2426
final class OrderStateUpdateHandler implements OrderStateUpdateHandlerInterface
@@ -107,7 +109,7 @@ private function createProductEvents(
107109
$resource,
108110
$variant->getId(),
109111
$this->productEventPayloadBuilder->build($eventType, $variant, $email),
110-
sprintf('%s - %s', $product?->getName(), $variant->getName()),
112+
$this->getProductName($variant, $product),
111113
);
112114
}
113115
} catch (\Throwable $exception) {
@@ -116,4 +118,22 @@ private function createProductEvents(
116118
]);
117119
}
118120
}
121+
122+
public function getProductName(
123+
ProductVariantInterface $variant,
124+
?ProductInterface $product,
125+
): string {
126+
$productName = $product?->getName();
127+
$variantName = $variant->getName();
128+
129+
if (null === $productName) {
130+
return $variantName ?? 'Unknown product';
131+
}
132+
133+
if (null === $variantName) {
134+
return $productName;
135+
}
136+
137+
return sprintf('%s - %s', $productName, $variantName);
138+
}
119139
}

src/Updater/CustomerWithKeyUpdater.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public function updateWithUserKey(
9292
$email,
9393
UserApiInterface::EMAIL_PROPERTY,
9494
);
95-
9695
if (null !== $userByEmailFromForm &&
9796
false === array_key_exists(AbstractClient::ERROR, $userByEmailFromForm)
9897
) {
@@ -103,21 +102,17 @@ public function updateWithUserKey(
103102
);
104103

105104
$this->userApi->mergeUsers($apiAwareResource, $userByEmailFromForm['id'], [$userFoundByKey['id']]);
105+
106+
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName);
107+
108+
return $user;
106109
}
107110

108111
if (!isset($user) || false === array_key_exists(AbstractClient::ERROR, $user)) {
109112
$user = $this->userApi->createUser($apiAwareResource, $payload);
110113
}
111114

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.');
117-
}
118-
119-
$this->cookieManager->setUserComCookie($user['user_key']);
120-
$this->sendEvent($apiAwareResource, $user['email'], $eventName);
115+
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName);
121116

122117
return $user;
123118
}
@@ -136,22 +131,44 @@ private function updateForUserWithoutEmail(
136131
UserApiInterface::EMAIL_PROPERTY,
137132
);
138133

139-
$id = null !== $customerFoundByEmail ?
140-
$customerFoundByEmail['id'] :
141-
$userFromUserKey['id'];
134+
$customerFoundByEmailId = null !== $customerFoundByEmail && isset($customerFoundByEmail['id'])
135+
? $customerFoundByEmail['id']
136+
: null
137+
;
138+
139+
$id = $customerFoundByEmailId ?? $userFromUserKey['id'];
142140

143141
$user = $this->userApi->updateUser(
144142
$apiAwareResource,
145143
$id,
146144
$this->buildPayload($email, $customer, $address),
147145
);
148146

149-
if (null !== $customerFoundByEmail) {
150-
$this->userApi->mergeUsers($apiAwareResource, $id, [$userFromUserKey]);
147+
if (is_array($customerFoundByEmail) &&
148+
array_key_exists('id', $customerFoundByEmail) &&
149+
array_key_exists('id', $userFromUserKey)
150+
) {
151+
$this->userApi->mergeUsers($apiAwareResource, $customerFoundByEmail['id'], [$userFromUserKey['id']]);
151152
}
152153

153154
$this->sendEvent($apiAwareResource, $email, $eventName);
154155

155156
return $user;
156157
}
158+
159+
public function changeCookieWithEvent(
160+
?array $user,
161+
UserComApiAwareInterface $apiAwareResource,
162+
string $eventName
163+
): void {
164+
if (false === is_array($user) ||
165+
false === array_key_exists('id', $user) ||
166+
false === array_key_exists('user_key', $user)
167+
) {
168+
throw new \RuntimeException('User might not be created or updated properly.');
169+
}
170+
171+
$this->cookieManager->setUserComCookie($user['user_key']);
172+
$this->sendEvent($apiAwareResource, $user['email'], $eventName);
173+
}
157174
}

src/Updater/CustomerWithoutKeyUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function updateWithoutUserKey(
3939
): array|null {
4040
$userApiAwareResource = $this->userComApiAwareResourceProvider->getApiAwareResource();
4141
if (null === $userApiAwareResource) {
42-
return null;
42+
throw new \RuntimeException('User.com API resource could not be found.');
4343
}
4444

4545
$email = $this->getEmail($customer, $email);

0 commit comments

Comments
 (0)