Passed
Pull Request — master (#20)
by Ronan
03:42
created

StatusCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 31
dl 0
loc 54
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A execute() 0 28 6
1
<?php
2
3
namespace App\Console\Command\User;
4
5
use App\Model\User;
6
use Ronanchilvers\Orm\Orm;
7
use RuntimeException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Command to create users
16
 *
17
 * @author Ronan Chilvers <[email protected]>
18
 */
19
class StatusCommand extends Command
20
{
21
    /**
22
     * @author Ronan Chilvers <[email protected]>
23
     */
24
    public function configure()
25
    {
26
        $this
27
            ->setName('user:status')
28
            ->setDescription('Update the status for an existing user')
29
            ->addArgument(
30
                'email',
31
                InputArgument::REQUIRED,
32
                'The email address of the user'
33
            )
34
            ->addArgument(
35
                'action',
36
                InputArgument::REQUIRED,
37
                'The action to take - one of \'activate\' or \'deactivate\''
38
            )
39
            ;
40
    }
41
42
    /**
43
     * @author Ronan Chilvers <[email protected]>
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $email = trim($input->getArgument('email'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('email') can also be of type string[]; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $email = trim(/** @scrutinizer ignore-type */ $input->getArgument('email'));
Loading history...
48
        $action = trim($input->getArgument('action'));
49
50
        if (!in_array($action, ['activate', 'deactivate'])) {
51
            throw new RuntimeException('Invalid action ' . $action);
52
        }
53
54
        $output->writeln('Email : ' . $email);
55
        $output->writeln('Action : ' . $action);
56
57
        $user = Orm::finder(User::class)->select()
58
            ->where(User::prefix('email'), $email)
59
            ->one();
60
        if (!$user instanceof User) {
61
            throw new RuntimeException('User not found for email ' . $email);
62
        }
63
        $user->status = ($action == 'activate') ? User::STATUS_ACTIVE : User::STATUS_INACTIVE;
64
        if (!$user->saveWithValidation()) {
65
            $errors = [];
66
            foreach ($user->getErrors() as $fieldErrors) {
67
                $errors = array_merge($errors, $fieldErrors);
68
            }
69
            $errors = implode(',', $errors);
70
            throw new RuntimeException('Unable to save user - ' . $errors);
71
        }
72
        $output->writeln("User {$email} updated");
73
    }
74
}
75