Test Failed
Branch master (7608ba)
by Florian
11:22
created

ResolverCollection::getPolicy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 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 Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
19
20
/**
21
 * `ResolverCollection` is used for aggregating multiple resolvers when more than
22
 * one resolver is necessary. The collection will iterate over configured resolvers
23
 * and try to resolve a policy on each one. The first successfully resolved policy
24
 * will be returned.
25
 *
26
 * Configured resolvers must throw `Authorization\Policy\Exception\MissingPolicyException`
27
 * if a policy cannot be resolved.
28
 *
29
 * Example configuration:
30
 *
31
 * ```
32
 * $collection = new ResolverCollection([
33
 *     new OrmResolver(),
34
 *     new MapResolver([
35
 *         Service::class => ServicePolicy::class
36
 *     ])
37
 * ]);
38
 *
39
 * $service = new AuthenticationService($collection);
40
 * ```
41
 */
42
class ResolverCollection implements ResolverCollectionInterface
43
{
44
    /**
45
     * Policy resolver instances.
46
     *
47
     * @var \Phauthentic\Authorization\Policy\ResolverInterface[]
48
     */
49
    protected $resolvers = [];
50
51
    /**
52
     * Constructor. Takes an array of policy resolver instances.
53
     *
54
     * @param \Phauthentic\Authorization\Policy\ResolverInterface[] $resolvers An array of policy resolver instances.
55
     */
56 1
    public function __construct(array $resolvers = [])
57
    {
58 1
        foreach ($resolvers as $resolver) {
59
            $this->add($resolver);
60
        }
61 1
    }
62
63
    /**
64
     * Adds a resolver to the collection.
65
     *
66
     * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Resolver instance.
67
     * @return $this
68
     */
69
    public function add(ResolverInterface $resolver): ResolverCollectionInterface
70
    {
71
        $this->resolvers[] = $resolver;
72
73
        return $this;
74
    }
75
}
76