StatusCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 2
cp 0
rs 9.9
cc 1
nc 1
nop 0
crap 2
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 = $input->getArgument('email');
48
        $email = trim($email);
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type null and string[]; however, parameter $string 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

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