PasswordManagement::updatePassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File:PasswordManagement.php
6
 *
7
 * @author Maciej Sławik <[email protected]>
8
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
9
 */
10
11
namespace LizardMedia\PasswordMigrator\Model;
12
13
use Exception;
14
use LizardMedia\PasswordMigrator\Api\Data\PasswordRepositoryInterface;
15
use LizardMedia\PasswordMigrator\Api\PasswordManagementInterface;
16
use Magento\Customer\Api\AccountManagementInterface;
17
use Magento\Customer\Api\CustomerRepositoryInterface;
18
use Magento\Customer\Model\AccountManagement;
19
use Magento\Customer\Model\CustomerRegistry;
20
use Magento\Framework\Exception\InputException;
21
use Magento\Framework\Exception\LocalizedException;
22
use Magento\Framework\Exception\NoSuchEntityException;
23
24
/**
25
 * Class PasswordManagement
26
 * @package LizardMedia\PasswordMigrator\Model
27
 */
28
class PasswordManagement implements PasswordManagementInterface
29
{
30
    /**
31
     * @var PasswordRepositoryInterface
32
     */
33
    private $passwordRepository;
34
35
    /**
36
     * @var CustomerRepositoryInterface
37
     */
38
    private $customerRepository;
39
40
    /**
41
     * @var AccountManagementInterface
42
     */
43
    private $accountManagement;
44
45
    /**
46
     * @var CustomerRegistry
47
     */
48
    private $customerRegistry;
49
50
    /**
51
     * PasswordManagement constructor.
52
     * @param PasswordRepositoryInterface $passwordRepository
53
     * @param CustomerRepositoryInterface $customerRepository
54
     * @param AccountManagementInterface $accountManagement
55
     * @param CustomerRegistry $customerRegistry
56
     */
57
    public function __construct(
58
        PasswordRepositoryInterface $passwordRepository,
59
        CustomerRepositoryInterface $customerRepository,
60
        AccountManagementInterface $accountManagement,
61
        CustomerRegistry $customerRegistry
62
    ) {
63
        $this->passwordRepository = $passwordRepository;
64
        $this->customerRepository = $customerRepository;
65
        $this->accountManagement = $accountManagement;
66
        $this->customerRegistry = $customerRegistry;
67
    }
68
69
    /**
70
     * @param int $customerId
71
     * @param string $newPassword
72
     * @return void
73
     * @throws InputException
74
     */
75
    public function updateCustomerPassword(int $customerId, string $newPassword): void
76
    {
77
        try {
78
            $this->generateNewResetToken($customerId);
79
            $this->updatePassword($customerId, $newPassword);
80
            $this->removeLegacyPassword($customerId);
81
        } catch (InputException $exception) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\InputException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
82
            throw $exception;
83
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
84
        }
85
    }
86
87
    /**
88
     * @param int $customerId
89
     * @throws LocalizedException
90
     * @throws NoSuchEntityException
91
     */
92
    private function generateNewResetToken(int $customerId): void
93
    {
94
        $customerData = $this->customerRepository->getById($customerId);
95
        try {
96
            $this->accountManagement->initiatePasswordReset($customerData->getEmail(), false);
97
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
98
        }
99
    }
100
101
    /**
102
     * @param int $customerId
103
     * @param string $newPassword
104
     * @throws LocalizedException
105
     * @throws NoSuchEntityException
106
     */
107
    private function updatePassword(int $customerId, string $newPassword): void
108
    {
109
        $customer = $this->customerRegistry->retrieve($customerId);
110
        $this->accountManagement->resetPassword(
111
            $customer->getData('email'),
112
            $customer->getData('rp_token'),
113
            $newPassword
114
        );
115
    }
116
117
    /**
118
     * @param int $customerId
119
     * @return void
120
     * @throws NoSuchEntityException
121
     * @throws Exception
122
     */
123
    private function removeLegacyPassword(int $customerId): void
124
    {
125
        $password = $this->passwordRepository->getByCustomerId($customerId);
126
        $this->passwordRepository->delete($password);
127
    }
128
}
129