Completed
Push — dev ( 088902...57261c )
by Matej
04:35
created

BaseCommand::checkRoles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.7899

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 5
cts 9
cp 0.5556
rs 9.4286
cc 3
eloc 9
nc 3
nop 0
crap 3.7899
1
<?php
2
3
namespace Velikonja\LabbyBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class BaseCommand
11
 *
12
 * @package Velikonja\LabbyBundle\Command
13
 * @author  Matej Velikonja <[email protected]>
14
 */
15
abstract class BaseCommand extends ContainerAwareCommand
16
{
17
    const ROLE_LOCAL  = 'local';
18
    const ROLE_REMOTE = 'remote';
19
20
    /**
21
     * @var array
22
     */
23
    private $roles = array();
24
25
    /**
26
     * Sets roles that are allowed to execute command.
27
     *
28
     * @param array $roles
29
     *
30
     * @return $this
31
     */
32 10
    public function setRoles(array $roles)
33
    {
34 10
        $this->roles = $roles;
35
36 10
        return $this;
37
    }
38
39 10
    public function run(InputInterface $input, OutputInterface $output)
40
    {
41 10
        $this->checkRoles();
42
43 10
        return parent::run($input, $output);
44
    }
45
46
    /**
47
     * TODO: check if this can be moved to event
48
     *
49
     * @throws \Exception
50
     *
51
     * @return bool
52
     */
53 10
    private function checkRoles()
54
    {
55 10
        $appRoles = $this->getContainer()->getParameter('velikonja_labby.config.roles');
56
57 10
        foreach ($appRoles as $appRole) {
58 10
            if (in_array($appRole, $this->roles)) {
59 10
                return true;
60
            }
61
        }
62
63
        throw new \Exception(sprintf(
64
            'App role ( %s ) is not allowed. Allowed roles: ( %s ).',
65
            implode(', ', $appRoles),
66
            implode(', ', $this->roles)
67
        ));
68
    }
69
}
70