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

AuthorizationService::can()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8.5786

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 38
ccs 15
cts 22
cp 0.6818
rs 8.6506
c 0
b 0
f 0
cc 7
nc 9
nop 3
crap 8.5786
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;
17
18
use Phauthentic\Authorization\Policy\BeforePolicyInterface;
19
use Phauthentic\Authorization\Policy\Exception\MissingMethodException;
20
use Phauthentic\Authorization\Policy\ResolverInterface;
21
use Phauthentic\Authorization\Policy\Result;
22
use Phauthentic\Authorization\Policy\ResultInterface;
23
use RuntimeException;
24
25
/**
26
 * Authorization Service
27
 */
28
class AuthorizationService implements AuthorizationServiceInterface
29
{
30
    /**
31
     * Authorization policy resolver.
32
     *
33
     * @var \Phauthentic\Authorization\Policy\ResolverInterface
34
     */
35
    protected $resolver;
36
37
    /**
38
     * Track whether or not authorization was checked.
39
     *
40
     * @var bool
41
     */
42
    protected $authorizationChecked = false;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Authorization policy resolver.
48
     */
49 15
    public function __construct(ResolverInterface $resolver)
50
    {
51 15
        $this->resolver = $resolver;
52 15
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57 11
    public function can(?IdentityInterface $user, string $action, $resource): ResultInterface
58
    {
59 11
        $this->authorizationChecked = true;
60 11
        $policy = $this->resolver->getPolicy($resource);
61
62 11
        if ($policy instanceof BeforePolicyInterface) {
63 5
            $result = $policy->before($user, $resource, $action);
64
65 5
            if (is_bool($result)) {
66
                return new Result($result);
67
            }
68
69 5
            if ($result instanceof ResultInterface) {
70 4
                return $result;
71
            }
72
73 1
            if ($result !== null) {
0 ignored issues
show
introduced by
The condition $result !== null is always false.
Loading history...
74
                throw new RuntimeException(sprintf(
75
                    'Pre-authorization check must return instance of `%s`, `bool` or `null`.',
76
                    ResultInterface::class
77
                ));
78
            }
79
        }
80
81 7
        $handler = $this->getCanHandler($policy, $action);
82 6
        $result = $handler($user, $resource);
83
84 6
        if (is_bool($result)) {
85 3
            return new Result($result);
86
        }
87
88 3
        if ($result instanceof ResultInterface) {
89 3
            return $result;
90
        }
91
92
        throw new RuntimeException(sprintf(
93
            'Policy action handler must return instance of `%s` or `bool`.',
94
            ResultInterface::class
95
        ));
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 3
    public function applyScope(?IdentityInterface $user, string $action, $resource)
102
    {
103 3
        $this->authorizationChecked = true;
104 3
        $policy = $this->resolver->getPolicy($resource);
105 3
        $handler = $this->getScopeHandler($policy, $action);
106
107 2
        return $handler($user, $resource);
108
    }
109
110
    /**
111
     * Returns a policy action handler.
112
     *
113
     * @param mixed $policy Policy object.
114
     * @param string $action Action name.
115
     * @return callable
116
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException
117
     */
118 7
    protected function getCanHandler($policy, $action): callable
119
    {
120 7
        $method = 'can' . ucfirst($action);
121
122 7
        if (!method_exists($policy, $method) && !method_exists($policy, '__call')) {
123 1
            throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]);
124
        }
125
126 6
        return [$policy, $method];
127
    }
128
129
    /**
130
     * Returns a policy scope action handler.
131
     *
132
     * @param mixed $policy Policy object.
133
     * @param string $action Action name.
134
     * @return callable
135
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException
136
     */
137 3
    protected function getScopeHandler($policy, $action): callable
138
    {
139 3
        $method = 'scope' . ucfirst($action);
140
141 3
        if (!method_exists($policy, $method)) {
142 1
            throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]);
143
        }
144
145 2
        return [$policy, $method];
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151 3
    public function authorizationChecked(): bool
152
    {
153 3
        return $this->authorizationChecked;
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159 1
    public function skipAuthorization(): AuthorizationServiceInterface
160
    {
161 1
        $this->authorizationChecked = true;
162
163 1
        return $this;
164
    }
165
}
166