Passed
Push — master ( 33548f...1e367d )
by Maciej
03:37 queued 01:03
created

AccountManagement::aroundResetPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 5
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,
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Magento\Framework\Exception\NoSuchEntityException 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...
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);
0 ignored issues
show
Unused Code introduced by
$customer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
}