Completed
Push — master ( 1486ea...92bb9e )
by Christian
03:40
created

ChangePasswordCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 93
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 15 1
A inject() 0 11 1
B execute() 0 31 5
1
<?php
2
3
namespace N98\Magento\Command\Customer;
4
5
use Exception;
6
use Magento\Framework\App\Area;
7
use Magento\Framework\Exception\NoSuchEntityException;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ChangePasswordCommand extends AbstractCustomerCommand
13
{
14
    /**
15
     * @var \Magento\Framework\App\State
16
     */
17
    private $state;
18
19
    /**
20
     * @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
21
     */
22
    private $customerRepository;
23
24
    /**
25
     * @var \Magento\Customer\Model\CustomerRegistry $customerRegistry
26
     */
27
    private $customerRegistry;
28
29
    /**
30
     * @var \Magento\Framework\Encryption\EncryptorInterface
31
     */
32
    private $encryptor;
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('customer:change-password')
38
            ->addArgument('email', InputArgument::OPTIONAL, 'Email')
39
            ->addArgument('password', InputArgument::OPTIONAL, 'Password')
40
            ->addArgument('website', InputArgument::OPTIONAL, 'Website of the customer')
41
            ->setDescription('Changes the password of a customer.')
42
        ;
43
44
        $help = <<<HELP
45
- Website parameter must only be given if more than one websites are available.
46
HELP;
47
        $this->setHelp($help);
48
    }
49
50
    /**
51
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
52
     * @param \Magento\Customer\Model\CustomerRegistry $customerRegistry
53
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
54
     */
55
    public function inject(
56
        \Magento\Framework\App\State $state,
57
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
58
        \Magento\Customer\Model\CustomerRegistry $customerRegistry,
59
        \Magento\Framework\Encryption\EncryptorInterface $encryptor
60
    ) {
61
        $this->state = $state;
62
        $this->customerRepository = $customerRepository;
63
        $this->customerRegistry = $customerRegistry;
64
        $this->encryptor = $encryptor;
65
    }
66
67
    /**
68
     * @param InputInterface  $input
69
     * @param OutputInterface $output
70
     *
71
     * @return int|void
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $this->detectMagento($output);
76
        if ($this->initMagento()) {
77
            $dialog = $this->getHelperSet()->get('dialog');
78
            $email = $this->getHelper('parameter')->askEmail($input, $output);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method askEmail() does only exist in the following implementations of said interface: N98\Util\Console\Helper\ParameterHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
79
80
            // Password
81
            if (($password = $input->getArgument('password')) == null) {
82
                $password = $dialog->ask($output, '<question>Password:</question>');
83
            }
84
85
            $website = $this->getHelper('parameter')->askWebsite($input, $output);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method askWebsite() does only exist in the following implementations of said interface: N98\Util\Console\Helper\ParameterHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
87
            $changePassword = function () use ($email, $website, $password) {
88
                $customer = $this->customerRepository->get($email, $website->getId());
89
                $passwordHash = $this->encryptor->getHash($password, true);
90
                $this->customerRepository->save($customer, $passwordHash);
91
            };
92
93
            try {
94
                $this->state->emulateAreaCode(Area::AREA_FRONTEND, $changePassword);
95
96
                $output->writeln('<info>Password successfully changed</info>');
97
            } catch (NoSuchEntityException $e) {
0 ignored issues
show
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...
98
                $output->writeln('<error>Customer could not be found.</error>');
99
            } catch (Exception $e) {
100
                $output->writeln('<error>' . $e->getMessage() . '</error>');
101
            }
102
        }
103
    }
104
}
105