1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: AccountManagement.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LizardMedia\PasswordMigrator\Plugin\Model; |
13
|
|
|
|
14
|
|
|
use LizardMedia\PasswordMigrator\Api\Data\PasswordRepositoryInterface; |
15
|
|
|
use Magento\Customer\Model\AccountManagement as MagentoAccountManagement; |
16
|
|
|
use Magento\Customer\Model\Customer; |
17
|
|
|
use Magento\Customer\Model\CustomerRegistry; |
18
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AccountManagement |
23
|
|
|
* @package LizardMedia\PasswordMigrator\Plugin\Model |
24
|
|
|
*/ |
25
|
|
|
class AccountManagement |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var PasswordRepositoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $passwordRepository; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var CustomerRegistry |
34
|
|
|
*/ |
35
|
|
|
private $customerRegistry; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
private $logger; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* PasswordManagement constructor. |
44
|
|
|
* @param PasswordRepositoryInterface $passwordRepository |
45
|
|
|
* @param CustomerRegistry $customerRegistry |
46
|
|
|
* @param LoggerInterface $logger |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
PasswordRepositoryInterface $passwordRepository, |
50
|
|
|
CustomerRegistry $customerRegistry, |
51
|
|
|
LoggerInterface $logger |
52
|
|
|
) { |
53
|
|
|
$this->passwordRepository = $passwordRepository; |
54
|
|
|
$this->customerRegistry = $customerRegistry; |
55
|
|
|
$this->logger = $logger; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param MagentoAccountManagement $subject |
61
|
|
|
* @param callable $proceed |
62
|
|
|
* @param $email |
63
|
|
|
* @param $resetToken |
64
|
|
|
* @param $newPassword |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function aroundResetPassword( |
68
|
|
|
MagentoAccountManagement $subject, |
|
|
|
|
69
|
|
|
callable $proceed, |
70
|
|
|
$email, |
71
|
|
|
$resetToken, |
72
|
|
|
$newPassword |
73
|
|
|
) { |
74
|
|
|
$result = $proceed($email, $resetToken, $newPassword); |
75
|
|
|
|
76
|
|
|
if ($result === true) { |
77
|
|
|
$this->processRemovingLegacyPassword($email); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $email |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
private function processRemovingLegacyPassword(string $email) : void |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
$customer = $this->getCustomer($email); |
92
|
|
|
$this->removeLegacyPasswordIfExists((int) $customer->getId()); |
93
|
|
|
} catch (NoSuchEntityException $exception) { |
|
|
|
|
94
|
|
|
} catch (\Exception $exception) { |
95
|
|
|
$this->logger->error($exception->getMessage()); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $email |
101
|
|
|
* @return Customer |
102
|
|
|
* @throws NoSuchEntityException |
103
|
|
|
*/ |
104
|
|
|
private function getCustomer(string $email) : Customer |
105
|
|
|
{ |
106
|
|
|
return $customer = $this->customerRegistry->retrieveByEmail($email); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $customerId |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
private function removeLegacyPasswordIfExists(int $customerId): void |
115
|
|
|
{ |
116
|
|
|
$password = $this->passwordRepository->getByCustomerId($customerId); |
117
|
|
|
$this->passwordRepository->delete($password); |
118
|
|
|
} |
119
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.