Skip to content

Commit 120420c

Browse files
committed
refactored login after steps to remove dry and to easeup future implementation of different logins
1 parent 768b723 commit 120420c

File tree

3 files changed

+73
-57
lines changed

3 files changed

+73
-57
lines changed

src/Model/Mutation/Login/LoginMutation.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Shopsys\FrontendApiBundle\Model\Mutation\Login;
66

77
use Overblog\GraphQLBundle\Definition\Argument;
8-
use Ramsey\Uuid\Uuid;
98
use Shopsys\FrameworkBundle\Model\Customer\User\FrontendCustomerUserProvider;
109
use Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade;
1110
use Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade;
@@ -15,6 +14,7 @@
1514
use Shopsys\FrontendApiBundle\Model\Mutation\AbstractMutation;
1615
use Shopsys\FrontendApiBundle\Model\Mutation\Customer\User\Exception\InvalidCredentialsUserError;
1716
use Shopsys\FrontendApiBundle\Model\Mutation\Customer\User\Exception\TooManyLoginAttemptsUserError;
17+
use Shopsys\FrontendApiBundle\Model\Security\LoginAsUserFacade;
1818
use Shopsys\FrontendApiBundle\Model\Security\LoginResultData;
1919
use Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory;
2020
use Shopsys\FrontendApiBundle\Model\Security\TokensDataFactory;
@@ -38,6 +38,7 @@ class LoginMutation extends AbstractMutation
3838
* @param \Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory $loginResultDataFactory
3939
* @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade $customerUserLoginTypeFacade
4040
* @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory
41+
* @param \Shopsys\FrontendApiBundle\Model\Security\LoginAsUserFacade $loginAsUserFacade
4142
*/
4243
public function __construct(
4344
protected readonly FrontendCustomerUserProvider $frontendCustomerUserProvider,
@@ -51,6 +52,7 @@ public function __construct(
5152
protected readonly LoginResultDataFactory $loginResultDataFactory,
5253
protected readonly CustomerUserLoginTypeFacade $customerUserLoginTypeFacade,
5354
protected readonly CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory,
55+
protected readonly LoginAsUserFacade $loginAsUserFacade,
5456
) {
5557
}
5658

@@ -71,41 +73,25 @@ public function loginMutation(Argument $argument): LoginResultData
7173
}
7274

7375
try {
74-
$user = $this->frontendCustomerUserProvider->loadUserByUsername($input['email']);
75-
} catch (UserNotFoundException $e) {
76+
$customerUser = $this->frontendCustomerUserProvider->loadUserByUsername($input['email']);
77+
} catch (UserNotFoundException) {
7678
throw new InvalidCredentialsUserError('Log in failed.');
7779
}
7880

79-
if (!$this->userPasswordHasher->isPasswordValid($user, $input['password'])) {
81+
if (!$this->userPasswordHasher->isPasswordValid($customerUser, $input['password'])) {
8082
throw new InvalidCredentialsUserError('Log in failed.');
8183
}
8284

83-
if (array_key_exists('cartUuid', $input) && $input['cartUuid'] !== null) {
84-
if (array_key_exists('shouldOverwriteCustomerUserCart', $input) && $input['shouldOverwriteCustomerUserCart']) {
85-
$this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($input['cartUuid'], $user);
86-
} else {
87-
$this->mergeCartFacade->mergeCartByUuidToCustomerCart($input['cartUuid'], $user);
88-
}
89-
}
90-
91-
$deviceId = Uuid::uuid4()->toString();
92-
9385
$this->loginRateLimiter->reset($this->requestStack->getCurrentRequest());
9486

95-
if (array_key_exists('productListsUuids', $input) && $input['productListsUuids']) {
96-
$this->productListFacade->mergeProductListsToCustomerUser($input['productListsUuids'], $user);
97-
}
98-
99-
$this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes(
100-
$this->customerUserLoginTypeDataFactory->create($user, LoginTypeEnum::WEB),
101-
);
102-
103-
return $this->loginResultDataFactory->create(
104-
$this->tokensDataFactory->create(
105-
$this->tokenFacade->createAccessTokenAsString($user, $deviceId),
106-
$this->tokenFacade->createRefreshTokenAsString($user, $deviceId),
107-
),
108-
$this->mergeCartFacade->shouldShowCartMergeInfo(),
87+
return $this->loginAsUserFacade->runLoginSteps(
88+
$customerUser,
89+
LoginTypeEnum::WEB,
90+
false,
91+
$input['productListsUuids'] ?? [],
92+
$input['shouldOverwriteCustomerUserCart'] ?? false,
93+
$input['cartUuid'] ?? null,
94+
null,
10995
);
11096
}
11197
}

src/Model/Security/LoginAsUserFacade.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Shopsys\FrameworkBundle\Model\Administrator\Security\AdministratorFrontSecurityFacade;
1111
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
1212
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserRepository;
13+
use Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade;
1314
use Shopsys\FrameworkBundle\Model\Security\Exception\LoginAsRememberedUserException;
15+
use Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade;
1416
use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory;
1517
use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade;
1618
use Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\LoginTypeEnum;
@@ -31,6 +33,9 @@ class LoginAsUserFacade
3133
* @param \Shopsys\FrontendApiBundle\Model\Security\TokensDataFactory $tokensDataFactory
3234
* @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeFacade $customerUserLoginTypeFacade
3335
* @param \Shopsys\FrontendApiBundle\Model\Customer\User\LoginType\CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory
36+
* @param \Shopsys\FrontendApiBundle\Model\Security\LoginResultDataFactory $loginResultDataFactory
37+
* @param \Shopsys\FrameworkBundle\Model\Product\List\ProductListFacade $productListFacade
38+
* @param \Shopsys\FrontendApiBundle\Model\Cart\MergeCartFacade $mergeCartFacade
3439
*/
3540
public function __construct(
3641
protected readonly CustomerUserRepository $customerUserRepository,
@@ -42,6 +47,9 @@ public function __construct(
4247
protected readonly TokensDataFactory $tokensDataFactory,
4348
protected readonly CustomerUserLoginTypeFacade $customerUserLoginTypeFacade,
4449
protected readonly CustomerUserLoginTypeDataFactory $customerUserLoginTypeDataFactory,
50+
protected readonly LoginResultDataFactory $loginResultDataFactory,
51+
protected readonly ProductListFacade $productListFacade,
52+
protected readonly MergeCartFacade $mergeCartFacade,
4553
) {
4654
}
4755

@@ -108,4 +116,46 @@ public function loginAndReturnAccessAndRefreshToken(CustomerUser $customerUser):
108116
$this->tokenFacade->createRefreshTokenAsString($customerUser, $deviceId),
109117
);
110118
}
119+
120+
/**
121+
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
122+
* @param string $loginType
123+
* @param bool $isRegistration
124+
* @param array $productListsUuids
125+
* @param bool $shouldOverwriteCustomerUserCart
126+
* @param string|null $cartUuid
127+
* @param string|null $externalId
128+
* @return \Shopsys\FrontendApiBundle\Model\Security\LoginResultData
129+
*/
130+
public function runLoginSteps(
131+
CustomerUser $customerUser,
132+
string $loginType,
133+
bool $isRegistration,
134+
array $productListsUuids,
135+
bool $shouldOverwriteCustomerUserCart,
136+
?string $cartUuid,
137+
?string $externalId,
138+
): LoginResultData {
139+
if ($cartUuid !== null) {
140+
if ($shouldOverwriteCustomerUserCart) {
141+
$this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($cartUuid, $customerUser);
142+
} else {
143+
$this->mergeCartFacade->mergeCartByUuidToCustomerCart($cartUuid, $customerUser);
144+
}
145+
}
146+
147+
if (count($productListsUuids) > 0) {
148+
$this->productListFacade->mergeProductListsToCustomerUser($productListsUuids, $customerUser);
149+
}
150+
151+
$this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes(
152+
$this->customerUserLoginTypeDataFactory->create($customerUser, $loginType, $externalId),
153+
);
154+
155+
return $this->loginResultDataFactory->create(
156+
$this->loginAndReturnAccessAndRefreshToken($customerUser),
157+
$this->mergeCartFacade->shouldShowCartMergeInfo(),
158+
$isRegistration,
159+
);
160+
}
111161
}

src/Model/SocialNetwork/SocialNetworkFacade.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,40 +92,20 @@ public function login(string $type, string $redirectUrl, SessionInterface $sessi
9292
}
9393
$adapter->disconnect();
9494

95-
$cartUuid = $session->get(SocialNetworkController::CART_UUID);
96-
$shouldOverwriteCustomerUserCart = $session->get(SocialNetworkController::SHOULD_OVERWRITE_CART);
97-
98-
$showCartMergeInfo = false;
99-
100-
if ($cartUuid !== null) {
101-
if ($shouldOverwriteCustomerUserCart) {
102-
$this->mergeCartFacade->overwriteCustomerCartWithCartByUuid($cartUuid, $customerUser);
103-
} else {
104-
$this->mergeCartFacade->mergeCartByUuidToCustomerCart($cartUuid, $customerUser);
105-
$showCartMergeInfo = true;
106-
}
107-
}
108-
109-
$productListsUuids = $session->get(SocialNetworkController::PRODUCT_LIST_UUIDS);
110-
111-
if ($productListsUuids !== null) {
112-
$this->productListFacade->mergeProductListsToCustomerUser(explode(',', $productListsUuids), $customerUser);
113-
}
95+
$loginResultData = $this->loginAsUserFacade->runLoginSteps(
96+
$customerUser,
97+
$type,
98+
$isRegistration,
99+
explode(',', $session->get(SocialNetworkController::PRODUCT_LIST_UUIDS)),
100+
$session->get(SocialNetworkController::SHOULD_OVERWRITE_CART),
101+
$session->get(SocialNetworkController::CART_UUID),
102+
(string)$userProfile->identifier,
103+
);
114104

115105
$session->remove(SocialNetworkController::CART_UUID);
116106
$session->remove(SocialNetworkController::SHOULD_OVERWRITE_CART);
117107
$session->remove(SocialNetworkController::PRODUCT_LIST_UUIDS);
118108

119-
$loginResultData = $this->loginResultDataFactory->create(
120-
$this->loginAsUserFacade->loginAndReturnAccessAndRefreshToken($customerUser),
121-
$showCartMergeInfo,
122-
$isRegistration,
123-
);
124-
125-
$this->customerUserLoginTypeFacade->updateCustomerUserLoginTypes(
126-
$this->customerUserLoginTypeDataFactory->create($customerUser, $type, (string)$userProfile->identifier),
127-
);
128-
129109
return $loginResultData;
130110
} catch (InvalidArgumentException | UnexpectedValueException $exception) {
131111
$message = sprintf('Login via %s doesn\'t work', $type);

0 commit comments

Comments
 (0)