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

StringResourceResolver::getPolicy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11
 * @link          https://cakephp.org CakePHP(tm) Project
12
 * @since         1.0.0
13
 * @license       https://opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace Phauthentic\Authorization\Policy;
16
17
use Phauthentic\Authorization\Policy\Exception\MissingPolicyException;
18
use InvalidArgumentException;
19
20
/**
21
 * A resolver that will use a string resource to instantiate the policy based on it
22
 */
23
class StringResourceResolver implements ResolverInterface
24
{
25
    /**
26
     * {@inheritDoc}
27
     *
28
     * @throws \InvalidArgumentException When a resource is not an object.
29
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingPolicyException When a policy for a resource has not been defined.
30
     */
31
    public function getPolicy($resource)
32
    {
33
        if (!is_string($resource)) {
34
            $message = sprintf('Resource must be a string, `%s` given.', gettype($resource));
35
            throw new InvalidArgumentException($message);
36
        }
37
38
        if (!class_exists($resource)) {
39
            throw new MissingPolicyException(sprintf('Policy for `%s` has not been defined.', $resource));
40
        }
41
42
        return new $resource();
43
    }
44
}
45