Completed
Push — master ( 0ab6a2...d8ddb3 )
by Robbie
01:17
created

ChangeGroupsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace SilverLeague\Console\Command\Member;
4
5
use SilverLeague\Console\Command\SilverStripeCommand;
6
use SilverStripe\Security\Group;
7
use SilverStripe\Security\Member;
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\ChoiceQuestion;
12
13
/**
14
 * Change a member's assigned groups
15
 *
16
 * @package silverstripe-console
17
 * @author  Robbie Averill <[email protected]>
18
 */
19
class ChangeGroupsCommand extends SilverStripeCommand
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24 2
    protected function configure()
25
    {
26
        $this
27 2
            ->setName('member:change-groups')
28 2
            ->setDescription("Change a member's groups")
29 2
            ->addArgument('email', InputArgument::OPTIONAL, 'Email address');
30 2
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 2
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 2
        $email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
38 2
        $member = Member::get()->filter('email', $email)->first();
39 2
        if (!$member) {
40 1
            $output->writeln('<error>Member with email "' . $email . '" was not found.');
41 1
            return;
42
        }
43
44 1
        if ($member->Groups()->count()) {
45
            $output->writeln(
46
                'Member <info>' . $email . '</info> is already in the following groups (will be overwritten):'
47
            );
48
            $output->writeln('   ' . implode(', ', $member->Groups()->column('Code')));
49
            $output->writeln('');
50
        }
51
52 1
        $allGroups = Group::get()->column('Code');
53 1
        $question = new ChoiceQuestion('Select the groups to add this Member to', $allGroups);
54 1
        $question->setMultiselect(true);
55
56 1
        $newGroups = $this->getHelper('question')->ask($input, $output, $question);
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...
57
58 1
        $output->writeln('Adding <info>' . $email . '</info> to groups: ' . implode(', ', $newGroups));
59
        // $member->Groups()->removeAll();
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60 1
        foreach ($newGroups as $group) {
61 1
            $member->addToGroupByCode($group);
62
        }
63
64 1
        $output->writeln('<info>Groups updated.</info>');
65 1
    }
66
}
67