Passed
Pull Request — develop (#2)
by AD
01:29
created

AuthorizationService::getScopeHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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\ResultInterface;
22
use RuntimeException;
23
24
/**
25
 * Authorization Service
26
 */
27
class AuthorizationService implements AuthorizationServiceInterface
28
{
29
    /**
30
     * Authorization policy resolver.
31
     *
32
     * @var \Phauthentic\Authorization\Policy\ResolverInterface
33
     */
34
    protected $resolver;
35
36
    /**
37
     * Track whether or not authorization was checked.
38
     *
39
     * @var bool
40
     */
41
    protected $authorizationChecked = false;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param \Phauthentic\Authorization\Policy\ResolverInterface $resolver Authorization policy resolver.
47
     */
48 15
    public function __construct(ResolverInterface $resolver)
49
    {
50 15
        $this->resolver = $resolver;
51 15
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 11
    public function can(?IdentityInterface $user, string $action, $resource)
57
    {
58 11
        $this->authorizationChecked = true;
59 11
        $policy = $this->resolver->getPolicy($resource);
60
61 11
        if ($policy instanceof BeforePolicyInterface) {
62 5
            $result = $policy->before($user, $resource, $action);
63
64 5
            if ($result instanceof ResultInterface || is_bool($result)) {
65 4
                return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type Phauthentic\Authorization\Policy\ResultInterface which is incompatible with the return type mandated by Phauthentic\Authorizatio...ServiceInterface::can() of boolean.
Loading history...
66
            }
67 1
            if ($result !== null) {
0 ignored issues
show
introduced by
The condition $result !== null is always false.
Loading history...
68
                throw new RuntimeException('Pre-authorization check must return `bool` or `null`.');
69
            }
70
        }
71
72 7
        $handler = $this->getCanHandler($policy, $action);
73 6
        $result = $handler($user, $resource);
74
75 6
        if ($result instanceof ResultInterface) {
76 1
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Phauthentic\Authorization\Policy\ResultInterface which is incompatible with the return type mandated by Phauthentic\Authorizatio...ServiceInterface::can() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
77
        }
78
79 5
        return $result === true;
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 3
    public function applyScope(?IdentityInterface $user, string $action, $resource)
86
    {
87 3
        $this->authorizationChecked = true;
88 3
        $policy = $this->resolver->getPolicy($resource);
89 3
        $handler = $this->getScopeHandler($policy, $action);
90
91 2
        return $handler($user, $resource);
92
    }
93
94
    /**
95
     * Returns a policy action handler.
96
     *
97
     * @param mixed $policy Policy object.
98
     * @param string $action Action name.
99
     * @return callable
100
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException
101
     */
102 7
    protected function getCanHandler($policy, $action)
103
    {
104 7
        $method = 'can' . ucfirst($action);
105
106 7
        if (!method_exists($policy, $method) && !method_exists($policy, '__call')) {
107 1
            throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]);
108
        }
109
110 6
        return [$policy, $method];
111
    }
112
113
    /**
114
     * Returns a policy scope action handler.
115
     *
116
     * @param mixed $policy Policy object.
117
     * @param string $action Action name.
118
     * @return callable
119
     * @throws \Phauthentic\Authorization\Policy\Exception\MissingMethodException
120
     */
121 3
    protected function getScopeHandler($policy, $action): callable
122
    {
123 3
        $method = 'scope' . ucfirst($action);
124
125 3
        if (!method_exists($policy, $method)) {
126 1
            throw (new MissingMethodException())->setMessageVars([$method, $action, get_class($policy)]);
127
        }
128
129 2
        return [$policy, $method];
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 3
    public function authorizationChecked(): bool
136
    {
137 3
        return $this->authorizationChecked;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143 1
    public function skipAuthorization(): AuthorizationServiceInterface
144
    {
145 1
        $this->authorizationChecked = true;
146
147 1
        return $this;
148
    }
149
}
150