ContainerResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 10
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getPolicy() 0 13 3
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
namespace Phauthentic\Authorization\Policy;
18
19
use InvalidArgumentException;
20
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
21
use Psr\Container\ContainerInterface;
22
23
/**
24
 * Container Resolver
25
 */
26
class ContainerResolver implements ResolverInterface
27
{
28
    /**
29
     * Container
30
     *
31
     * @var \Psr\Container\ContainerInterface
32
     */
33
    protected $container;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param \Psr\Container\ContainerInterface $container PSR Container
39
     */
40
    public function __construct(ContainerInterface $container)
41
    {
42
        $this->container = $container;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * @throws \InvalidArgumentException When a resource is not a string.
49
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingPolicyException When a policy for a resource has not been defined.
50
     */
51
    public function getPolicy($resource)
52
    {
53
        if (!is_string($resource)) {
54
            $message = sprintf('Resource must be a string, `%s` given.', gettype($resource));
55
56
            throw new InvalidArgumentException($message);
57
        }
58
59
        if (!$this->container->has($resource)) {
60
            throw new MissingPolicyException(sprintf('Policy for `%s` has not been defined.', $resource));
61
        }
62
63
        return $this->container->get($resource);
64
    }
65
}
66