Passed
Pull Request — master (#1)
by Florian
02:02
created

CollectionResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 36
ccs 8
cts 11
cp 0.7272
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPolicy() 0 14 4
A __construct() 0 3 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 InvalidArgumentException;
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 CollectionResolver implements ResolverInterface
44
{
45
    /**
46
     * Policy resolver instances.
47
     *
48
     * @var \Phauthentic\Authorization\Policy\ResolverInterface[]
49
     */
50
    protected $collection;
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 1
    public function __construct(ResolverCollection $collection)
58
    {
59 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...
60 1
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 1
    public function getPolicy($resource)
66
    {
67 1
        foreach ($this->collection as $resolver) {
68
            try {
69 1
                return $resolver->getPolicy($resource);
70 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...
71 1
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
72
            }
73
        }
74
75
        $class = get_class($resource);
76
        $message = sprintf('Policy for `%s` has not been defined.', $class);
77
78
        throw new MissingPolicyException($message);
79
    }
80
}
81