CreateCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 61
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 11 1
B execute() 0 40 6
1
<?php
2
3
namespace SilverLeague\Console\Command\Member;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Security\Member;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
13
/**
14
 * Create a new member, and optionally add them to groups and roles.
15
 *
16
 * @package silverstripe-console
17
 * @author  Robbie Averill <[email protected]>
18
 */
19
class CreateCommand extends SilverStripeCommand
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24 3
    protected function configure()
25
    {
26
        $this
27 3
            ->setName('member:create')
28 3
            ->setDescription('Create a new member, and optionally add them to groups')
29 3
            ->addArgument('email', InputArgument::OPTIONAL, 'Email address')
30 3
            ->addArgument('username', InputArgument::OPTIONAL, 'Username')
31 3
            ->addArgument('password', InputArgument::OPTIONAL, 'Password')
32 3
            ->addArgument('firstname', InputArgument::OPTIONAL, 'First name')
33 3
            ->addArgument('surname', InputArgument::OPTIONAL, 'Surname');
34 3
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 1
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $data = [
42 1
            'Email'     => $this->getOrAskForArgument($input, $output, 'email', 'Email address: '),
43 1
            'Password'  => $this->getOrAskForArgument($input, $output, 'password', 'Password: '),
44 1
            'FirstName' => $this->getOrAskForArgument($input, $output, 'firstname', 'First name: '),
45 1
            'Surname'   => $this->getOrAskForArgument($input, $output, 'surname', 'Surname: ')
46
        ];
47 1
        if (empty($data['Email']) || empty($data['Password'])) {
48
            $output->writeln('<error>Please enter an email address and password.</error>');
49
            return;
50
        }
51
52 1
        // Check for existing member
53 1
        $member = Member::get()->filter(['Email' => $data['Email']])->first();
54 1
        if ($member) {
55
            $output->writeln('<error>Member already exists with email address: ' . $data['Email']);
56 1
            return;
57
        }
58 1
59
        $member = Member::create();
60 1
        foreach ($data as $key => $value) {
61 1
            $member->setField($key, $value);
62
        }
63
        $member->write();
64
65
        $output->writeln('<info>Member created.</info>');
66
67
        $setGroups = new Question('Do you want to assign groups now? ', 'yes');
68
        if ($this->getHelper('question')->ask($input, $output, $setGroups) === 'yes') {
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 ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

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...
69
            $command = $this->getApplication()->find('member:change-groups');
70
            $command->run(
71 1
                new ArrayInput([
72
                    'command' => 'member:change-groups',
73
                    'email'   => $data['Email']
74
                ]),
75
                $output
76
            );
77
        }
78
    }
79
}
80