StringResourceResolver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 7
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPolicy() 0 12 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
declare(strict_types=1);
18
19
namespace Phauthentic\Authorization\Policy;
20
21
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
22
use InvalidArgumentException;
23
24
/**
25
 * A resolver that will use a string resource to instantiate the policy based on it
26
 */
27
class StringResourceResolver implements ResolverInterface
28
{
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @throws \InvalidArgumentException When a resource is not an object.
33
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingPolicyException When a policy for a resource has not been defined.
34
     */
35
    public function getPolicy($resource)
36
    {
37
        if (!is_string($resource)) {
38
            $message = sprintf('Resource must be a string, `%s` given.', gettype($resource));
39
            throw new InvalidArgumentException($message);
40
        }
41
42
        if (!class_exists($resource)) {
43
            throw new MissingPolicyException(sprintf('Policy for `%s` has not been defined.', $resource));
44
        }
45
46
        return new $resource();
47
    }
48
}
49