CollectionResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 1
b 0
f 0
dl 0
loc 37
ccs 7
cts 11
cp 0.6364
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPolicy() 0 15 4
A __construct() 0 3 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 InvalidArgumentException;
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 CollectionResolver implements ResolverInterface
47
{
48
    /**
49
     * Policy resolver instances.
50
     *
51
     * @var \Phauthentic\Authorization\Policy\ResolverCollectionInterface
52
     */
53
    protected $collection;
54
55
    /**
56
     * Constructor. Takes an array of policy resolver instances.
57
     *
58
     * @param \Phauthentic\Authorization\Policy\ResolverCollectionInterface $collection
59
     */
60 2
    public function __construct(ResolverCollectionInterface $collection)
61
    {
62 2
        $this->collection = $collection;
63 2
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 2
    public function getPolicy($resource)
69
    {
70 2
        foreach ($this->collection as $resolver) {
71
            try {
72 2
                return $resolver->getPolicy($resource);
73 2
            } catch (MissingPolicyException $e) {
74
                // Ignore this case because we'll try the next resolver
75
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
            }
77
        }
78
79
        $class = get_class($resource);
80
        $message = sprintf('Policy for `%s` has not been defined.', $class);
81
82
        throw new MissingPolicyException($message);
83
    }
84
}
85