SecurityAwareStateMachine::setSecurityContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Finite\StateMachine;
4
5
use Finite\Transition\TransitionInterface;
6
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
7
8
/**
9
 * Security Aware state machine.
10
 * Use the Symfony Security Component and ACL.
11
 *
12
 * Need an ACL implementation available, Doctrine DBAL by default.
13
 *
14
 * @author Yohan Giarelli <[email protected]>
15
 */
16
class SecurityAwareStateMachine extends StateMachine
17
{
18
    /**
19
     * @var AuthorizationCheckerInterface
20
     */
21
    protected $authorizationChecker;
22
23
    /**
24
     * @param AuthorizationCheckerInterface $authorizationChecker
25
     */
26 5
    public function setSecurityContext(AuthorizationCheckerInterface $authorizationChecker)
27
    {
28 5
        $this->authorizationChecker = $authorizationChecker;
29 5
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 5
    public function can($transition, array $parameters = array())
35
    {
36 5
        $transition = $transition instanceof TransitionInterface ? $transition : $this->getTransition($transition);
37
38 5
        if (!$this->authorizationChecker->isGranted($transition->getName(), $this->getObject())) {
39 5
            return false;
40
        }
41
42 5
        return parent::can($transition, $parameters);
43
    }
44
}
45