Completed
Push — master ( c98431...13ba71 )
by
unknown
11:13
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 constructor.
29
     *
30
     * @param StrategyInterface[] $strategies List of strategies to be applied.
31
     */
32 2
    public function __construct(array $strategies)
33
    {
34 2
        $this->strategies = $strategies;
35 2
    }
36
37
    /**
38
     * Applies the defined strategies on the provided request.
39
     *
40
     * @param Request $request request to handle
41
     *
42
     * @return string
43
     */
44
    public function apply(Request $request)
45
    {
46
        $exceptions = [];
47
48
        foreach ($this->strategies as $strategy) {
49
            try {
50
                $name = $strategy->apply($request);
51
                $this->roles = $strategy->getRoles();
52
53
                if ($name && $strategy->stopPropagation()) {
54
                    return $name;
55
                }
56
            } catch (\InvalidArgumentException $e) {
57
                $exceptions[] = $e;
58
            }
59
        }
60
61
        throw new \InvalidArgumentException($exceptions[0]->getMessage(), $exceptions[0]);
62
    }
63
64
    /**
65
     * Decider to stop other strategies running after from being considered.
66
     *
67
     * @return boolean
68
     */
69
    public function stopPropagation()
70
    {
71
        return false;
72
    }
73
74
    /**
75
     * Provides the list of registered roles.
76
     *
77
     * @return Role[]
78
     */
79
    public function getRoles()
80
    {
81
        return array_unique($this->roles);
82
    }
83
}
84