IdentityDecorator   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
wmc 19
eloc 26
c 0
b 0
f 0
dl 0
loc 154
ccs 31
cts 35
cp 0.8857
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A applyScope() 0 3 1
A __isset() 0 3 1
A getOriginalData() 0 7 4
A offsetExists() 0 3 1
A can() 0 3 1
A __get() 0 3 1
A __construct() 0 11 4
A offsetUnset() 0 3 1
A __call() 0 8 2
A offsetSet() 0 3 1
A offsetGet() 0 7 2
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;
20
21
use ArrayAccess;
22
use BadMethodCallException;
23
use InvalidArgumentException;
24
use Phauthentic\Authorization\Policy\ResultInterface;
25
26
/**
27
 * An decorator implementing the IdentityInterface.
28
 *
29
 * This decorator is intended to wrap the application defined identity
30
 * object and proxy attributes/methods to and 'mixin' the can() method.
31
 *
32
 * The decorated identity must implement ArrayAccess or be an array.
33
 */
34
class IdentityDecorator implements IdentityInterface
35
{
36
    /**
37
     * Identity data
38
     *
39
     * @var array|\ArrayAccess
40
     */
41
    protected $identity;
42
43
    /**
44
     * Authorization Service
45
     *
46
     * @var \Phauthentic\Authorization\AuthorizationServiceInterface
47
     */
48
    protected $authorization;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param \Phauthentic\Authorization\AuthorizationServiceInterface $service The authorization service.
54
     * @param array|\ArrayAccess $identity Identity data
55
     * @throws \InvalidArgumentException When invalid identity data is passed.
56
     */
57 50
    public function __construct(AuthorizationServiceInterface $service, $identity)
58
    {
59 50
        if (!is_array($identity) && !$identity instanceof ArrayAccess) {
0 ignored issues
show
introduced by
$identity is always a sub-type of ArrayAccess.
Loading history...
60 2
            $type = is_object($identity) ? get_class($identity) : gettype($identity);
61 2
            $message = sprintf('Identity data must be an `array` or implement `ArrayAccess` interface, `%s` given.', $type);
62
63 2
            throw new InvalidArgumentException($message);
64
        }
65
66 48
        $this->authorization = $service;
67 48
        $this->identity = $identity;
68 48
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73 2
    public function can($action, $resource): ResultInterface
74
    {
75 2
        return $this->authorization->can($this, $action, $resource);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 2
    public function applyScope($action, $resource)
82
    {
83 2
        return $this->authorization->applyScope($this, $action, $resource);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 8
    public function getOriginalData()
90
    {
91 8
        if ($this->identity && is_object($this->identity) && method_exists($this->identity, 'getOriginalData')) {
92 2
            return $this->identity->getOriginalData();
93
        }
94
95 8
        return $this->identity;
96
    }
97
98
    /**
99
     * Delegate unknown methods to decorated identity.
100
     *
101
     * @param string $method The method being invoked.
102
     * @param array $args The arguments for the method.
103
     * @return mixed
104
     */
105 4
    public function __call($method, $args)
106
    {
107 4
        if (!is_object($this->identity)) {
108 2
            throw new BadMethodCallException("Cannot call `{$method}`. Identity data is not an object.");
109
        }
110 2
        $call = [$this->identity, $method];
111
112 2
        return $call(...$args);
113
    }
114
115
    /**
116
     * Delegate property access to decorated identity.
117
     *
118
     * @param string $property The property to read.
119
     * @return mixed
120
     */
121
    public function __get($property)
122
    {
123
        return $this->identity->{$property};
124
    }
125
126
    /**
127
     * Delegate property isset to decorated identity.
128
     *
129
     * @param string $property The property to read.
130
     * @return mixed
131
     */
132
    public function __isset($property)
133
    {
134
        return isset($this->identity->{$property});
135
    }
136
137
    /**
138
     * Whether a offset exists
139
     *
140
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
141
     * @param mixed $offset Offset
142
     * @return bool
143
     */
144 4
    public function offsetExists($offset)
145
    {
146 4
        return isset($this->identity[$offset]);
147
    }
148
149
    /**
150
     * Offset to retrieve
151
     *
152
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
153
     * @param mixed $offset Offset
154
     * @return mixed
155
     */
156 16
    public function offsetGet($offset)
157
    {
158 16
        if (isset($this->identity[$offset])) {
159 14
            return $this->identity[$offset];
160
        }
161
162 4
        return null;
163
    }
164
165
    /**
166
     * Offset to set
167
     *
168
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
169
     * @param mixed $offset The offset to assign the value to.
170
     * @param mixed $value Value
171
     * @return mixed
172
     */
173 2
    public function offsetSet($offset, $value)
174
    {
175 2
        return $this->identity[$offset] = $value;
176
    }
177
178
    /**
179
     * Offset to unset
180
     *
181
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
182
     * @param mixed $offset Offset
183
     * @return void
184
     */
185 2
    public function offsetUnset($offset)
186
    {
187 2
        unset($this->identity[$offset]);
188 2
    }
189
}
190