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

CollectionResolver::__construct()   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 1
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 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 CollectionResolver implements ResolverInterface
43
{
44
    /**
45
     * Policy resolver instances.
46
     *
47
     * @var \Phauthentic\Authorization\Policy\ResolverInterface[]
48
     */
49
    protected $collection;
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(ResolverCollection $collection)
57
    {
58 1
        $this->collection = $collection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collection of type Phauthentic\Authorizatio...licy\ResolverCollection is incompatible with the declared type Phauthentic\Authorizatio...icy\ResolverInterface[] of property $collection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 1
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 1
    public function getPolicy($resource)
65
    {
66 1
        foreach ($this->collection as $resolver) {
67
            try {
68 1
                return $resolver->getPolicy($resource);
69 1
            } catch (MissingPolicyException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
            }
71
        }
72
73
        $class = get_class($resource);
74
        $message = sprintf('Policy for `%s` has not been defined.', $class);
75
76
        throw new MissingPolicyException($message);
77
    }
78
}
79