Completed
Pull Request — develop (#619)
by
unknown
04:25
created

MultiStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * strategy combining a set of strategies to be applied
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Security\Core\Role\Role;
10
11
/**
12
 * Class MultiStrategy
13
 *
14
 * @package Graviton\SecurityBundle\Authentication\Strategies
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class MultiStrategy implements StrategyInterface
20
{
21
    /** @var StrategyInterface[]  */
22
    private $strategies = [];
23
24
    /** @var Role[] */
25
    private $roles = [];
26
27
    /**
28
     * MultiStrategy add.
29
     *
30
     * @param StrategyInterface $strategy strategy to be applied.
31
     * @return void
32
     */
33 2
    public function addStrategy(StrategyInterface $strategy)
34
    {
35 2
        $this->strategies[] = $strategy;
36 2
    }
37
38
    /**
39
     * Applies the defined strategies on the provided request.
40
     *
41
     * @param Request $request request to handle
42
     *
43
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45 2
    public function apply(Request $request)
46
    {
47 2
        foreach ($this->strategies as $strategy) {
48 2
            $name = $strategy->apply($request);
49 2
            if ($strategy->stopPropagation()) {
50 2
                $this->roles = $strategy->getRoles();
51 2
                return $name;
52 1
            }
53 1
        }
54
55
        return false;
56
    }
57
58
    /**
59
     * Decider to stop other strategies running after from being considered.
60
     *
61
     * @return boolean
62
     */
63
    public function stopPropagation()
64
    {
65
        return false;
66
    }
67
68
    /**
69
     * Provides the list of registered roles.
70
     *
71
     * @return Role[]
72
     */
73 2
    public function getRoles()
74
    {
75 2
        return array_unique($this->roles);
76
    }
77
}
78