Test Failed
Push — master ( 7608ba...b20d8b )
by Florian
02:11
created

ResolverCollection::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
5
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
12
 * @link          https://cakephp.org CakePHP(tm) Project
13
 * @since         1.0.0
14
 * @license       https://opensource.org/licenses/mit-license.php MIT License
15
 */
16
namespace Phauthentic\Authorization\Policy;
17
18
use ArrayIterator;
19
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
20
21
/**
22
 * `ResolverCollection` is used for aggregating multiple resolvers when more than
23
 * one resolver is necessary. The collection will iterate over configured resolvers
24
 * and try to resolve a policy on each one. The first successfully resolved policy
25
 * will be returned.
26
 *
27
 * Configured resolvers must throw `Authorization\Policy\Exception\MissingPolicyException`
28
 * if a policy cannot be resolved.
29
 *
30
 * Example configuration:
31
 *
32
 * ```
33
 * $collection = new ResolverCollection([
34
 *     new OrmResolver(),
35
 *     new MapResolver([
36
 *         Service::class => ServicePolicy::class
37
 *     ])
38
 * ]);
39
 *
40
 * $service = new AuthenticationService($collection);
41
 * ```
42
 */
43
class ResolverCollection implements ResolverCollectionInterface
44
{
45
    /**
46
     * Policy resolver instances.
47
     *
48
     * @var \Phauthentic\Authorization\Policy\ResolverInterface[]
49
     */
50
    protected $resolvers = [];
51
52
    /**
53
     * Constructor. Takes an array of policy resolver instances.
54
     *
55
     * @param \Phauthentic\Authorization\Policy\ResolverInterface[] $resolvers An array of policy resolver instances.
56
     */
57 2
    public function __construct(array $resolvers = [])
58
    {
59 2
        foreach ($resolvers as $resolver) {
60 2
            $this->add($resolver);
61
        }
62 2
    }
63
64
    /**
65
     * Adds a resolver to the collection.
66
     *
67
     * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Resolver instance.
68
     * @return $this
69
     */
70 2
    public function add(ResolverInterface $resolver): ResolverCollectionInterface
71
    {
72 2
        $this->resolvers[] = $resolver;
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function count(): int
81
    {
82
        return count($this->resolvers);
83
    }
84
85
    /**
86
     * Gets the iterator
87
     *
88
     * @return \Iterator
89
     */
90 2
    public function getIterator()
91
    {
92 2
        return new ArrayIterator($this->resolvers);
93
    }
94
}
95