ResolverCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
wmc 8
eloc 14
c 0
b 0
f 0
dl 0
loc 64
ccs 9
cts 17
cp 0.5294
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A getIterator() 0 3 1
A getPolicy() 0 11 3
A count() 0 3 1
A add() 0 4 1
1
<?php
2
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
17
declare(strict_types=1);
18
19
namespace Phauthentic\Authorization\Policy;
20
21
use ArrayIterator;
22
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
23
24
/**
25
 * `ResolverCollection` is used for aggregating multiple resolvers when more than
26
 * one resolver is necessary. The collection will iterate over configured resolvers
27
 * and try to resolve a policy on each one. The first successfully resolved policy
28
 * will be returned.
29
 *
30
 * Configured resolvers must throw `Authorization\Policy\Exception\MissingPolicyException`
31
 * if a policy cannot be resolved.
32
 *
33
 * Example configuration:
34
 *
35
 * ```
36
 * $collection = new ResolverCollection([
37
 *     new OrmResolver(),
38
 *     new MapResolver([
39
 *         Service::class => ServicePolicy::class
40
 *     ])
41
 * ]);
42
 *
43
 * $service = new AuthenticationService($collection);
44
 * ```
45
 */
46
class ResolverCollection implements ResolverInterface, ResolverCollectionInterface
47
{
48
    /**
49
     * Policy resolver instances.
50
     *
51
     * @var \Phauthentic\Authorization\Policy\ResolverInterface[]
52
     */
53
    protected $resolvers = [];
54
/**
55
     * Constructor. Takes an array of policy resolver instances.
56
     *
57
     * @param \Phauthentic\Authorization\Policy\ResolverInterface[] $resolvers An array of policy resolver instances.
58
     */
59 4
    public function __construct(array $resolvers = [])
60
    {
61 4
        foreach ($resolvers as $resolver) {
62 4
            $this->add($resolver);
63
        }
64 4
    }
65
66
    /**
67
     * Adds a resolver to the collection.
68
     *
69
     * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Resolver instance.
70
     * @return $this
71
     */
72 4
    public function add(ResolverInterface $resolver): ResolverCollectionInterface
73
    {
74 4
        $this->resolvers[] = $resolver;
75 4
        return $this;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function count(): int
82
    {
83
        return count($this->resolvers);
84
    }
85
86
    /**
87
     * Gets the iterator
88
     *
89
     * @return \Iterator
90
     */
91 4
    public function getIterator()
92
    {
93 4
        return new ArrayIterator($this->resolvers);
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function getPolicy($resource)
100
    {
101
        foreach ($this->resolvers as $resolver) {
102
            try {
103
                return $resolver->getPolicy($resource);
104
            } catch (MissingPolicyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
105
            }
106
        }
107
108
        $exception = new MissingPolicyException();
109
        throw $exception->setMessageVars([get_class($resource)]);
110
    }
111
}
112