Completed
Push — master ( 97f6b5...9ef69c )
by Matze
08:25
created

CreateToken::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1.006

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
crap 1.006
1
<?php
2
3
namespace BrainExe\Core\Authentication\Command;
4
5
use BrainExe\Core\Annotations\Command as CommandAnnotation;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use BrainExe\Core\Authentication\Token as TokenModel;
11
12
/**
13
 * @CommandAnnotation
14
 */
15
class CreateToken extends Command
16
{
17
    /**
18
     * @var TokenModel
19
     */
20
    private $token;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    protected function configure()
26
    {
27 2
        $this->setName('token:create')
28 2
            ->setDescription('Create user token')
29 2
            ->addArgument('user', InputArgument::REQUIRED)
30 2
            ->addArgument('roles', InputArgument::OPTIONAL);
31 2
    }
32
33
    /**
34
     * @param TokenModel $token
35
     */
36 2
    public function __construct(TokenModel $token)
37
    {
38 2
        parent::__construct();
39
40 2
        $this->token = $token;
41 2
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 1
        $userId = (int)$input->getArgument('user');
49 1
        $roles  = (string)$input->getArgument('roles');
50
51 1
        $token = $this->token->addToken($userId, explode(',', $roles));
52 1
        $output->writeln(
53 1
            sprintf(
54 1
                'Created token %s for user %d with roles: %s',
55
                $token,
56
                $userId,
57 1
                $roles
58
            )
59
        );
60 1
    }
61
}
62