Completed
Pull Request — develop (#523)
by Bastian
14:39 queued 10:07
created

MultiStrategy::stopPropagation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
10
/**
11
 * Class MultiStrategy
12
 *
13
 * @package Graviton\SecurityBundle\Authentication\Strategies
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class MultiStrategy implements StrategyInterface
19
{
20
    /** @var StrategyInterface[]  */
21
    protected $strategies;
22
23
    /**
24
     * MultiStrategy constructor.
25
     *
26
     * @param StrategyInterface[] $strategies List of strategies to be applied.
27
     */
28 4
    public function __construct(array $strategies)
29
    {
30 4
        $this->strategies = $strategies;
31 4
    }
32
33
    /**
34
     * Applies the defined strategies on the provided request.
35
     *
36
     * @param Request $request request to handle
37
     *
38
     * @return string
39
     */
40
    public function apply(Request $request)
41
    {
42
        $exceptions = [];
43
44
        foreach ($this->strategies as $strategy) {
45
            try {
46
                $name = $strategy->apply($request);
47
48
                if ($strategy->stopPropagation()) {
49
                    return $name;
50
                }
51
            } catch (\InvalidArgumentException $e) {
52
                $exceptions[] = $e;
53
            }
54
        }
55
56
        throw new \InvalidArgumentException($exceptions[0]->getMessage(), $exceptions[0]);
57
    }
58
59
    /**
60
     * Decider to stop other strategies running after from being considered.
61
     *
62
     * @return boolean
63
     */
64
    public function stopPropagation()
65
    {
66
        return false;
67
    }
68
}
69