Skip to content

Commit fc3dd56

Browse files
authored
Merge pull request #11 from BitBagCommerce/bugfix/UC-18-adjustments-after-tests
Refactor user update flow & adjust name of product while creating deal
2 parents 767f840 + c091f76 commit fc3dd56

4 files changed

Lines changed: 63 additions & 18 deletions

File tree

src/Api/AbstractClient.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@ protected function request(
4444
);
4545

4646
$status = $response->getStatusCode();
47+
4748
if ($status === Response::HTTP_TOO_MANY_REQUESTS && !$retrial) {
4849
sleep(1);
4950

5051
return $this->request($path, $method, $options, true);
5152
}
5253

5354
if ($status >= Response::HTTP_OK && $status < Response::HTTP_MULTIPLE_CHOICES) {
55+
$this->logger->debug(sprintf(
56+
sprintf('%s User.com API request', $status),
57+
), [
58+
'path' => $path,
59+
'method' => $method,
60+
'options' => $options,
61+
'response' => $response->getContent(false),
62+
]);
63+
5464
return $response->toArray();
5565
}
5666

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: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

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)