AuthorizationRulesTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 190
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A reqHasAction() 0 4 1
A reqGetAction() 0 8 1
A reqHasResourceType() 0 4 1
A reqGetResourceType() 0 10 2
A reqHasResourceIdentity() 0 4 1
A reqGetResourceIdentity() 0 10 2
A reqHasResourceAttributes() 0 4 1
A reqGetResourceAttributes() 0 10 1
A reqHasResourceRelationships() 0 4 1
A reqGetResourceRelationships() 0 10 1
A ctxHasCurrentAccount() 0 9 1
A ctxGetCurrentAccount() 0 11 1
A ctxHasContainer() 0 4 1
A ctxGetContainer() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Authorization;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Auth\Contracts\Authorization\PolicyInformation\ContextInterface;
22
use Limoncello\Contracts\Authentication\AccountInterface;
23
use Limoncello\Contracts\Authentication\AccountManagerInterface;
24
use Psr\Container\ContainerExceptionInterface;
25
use Psr\Container\ContainerInterface;
26
use Psr\Container\NotFoundExceptionInterface;
27
use function assert;
28
use function is_array;
29
use function is_string;
30
31
/**
32
 * @package Limoncello\Application
33
 */
34
trait AuthorizationRulesTrait
35
{
36 1
    /**
37
     * @param ContextInterface $context
38 1
     *
39
     * @return bool
40
     */
41
    protected static function reqHasAction(ContextInterface $context): bool
42
    {
43
        return $context->getRequest()->has(RequestProperties::REQ_ACTION);
44
    }
45
46 1
    /**
47
     * @param ContextInterface $context
48 1
     *
49
     * @return string
50 1
     */
51
    protected static function reqGetAction(ContextInterface $context): string
52 1
    {
53
        assert(static::reqHasAction($context));
54
55
        $value = $context->getRequest()->get(RequestProperties::REQ_ACTION);
56
57
        return $value;
58
    }
59
60 1
    /**
61
     * @param ContextInterface $context
62 1
     *
63
     * @return bool
64
     */
65
    protected static function reqHasResourceType(ContextInterface $context): bool
66
    {
67
        return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_TYPE);
68
    }
69
70 1
    /**
71
     * @param ContextInterface $context
72 1
     *
73
     * @return string|null
74 1
     */
75
    protected static function reqGetResourceType(ContextInterface $context): ?string
76 1
    {
77
        assert(static::reqHasResourceType($context));
78 1
79
        $value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_TYPE);
80
81
        assert($value === null || is_string($value));
82
83
        return $value;
84
    }
85
86 1
    /**
87
     * @param ContextInterface $context
88 1
     *
89
     * @return bool
90
     */
91
    protected static function reqHasResourceIdentity(ContextInterface $context): bool
92
    {
93
        return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_IDENTITY);
94
    }
95
96 1
    /**
97
     * @param ContextInterface $context
98 1
     *
99
     * @return string|null
100 1
     */
101
    protected static function reqGetResourceIdentity(ContextInterface $context): ?string
102 1
    {
103
        assert(static::reqHasResourceIdentity($context));
104 1
105
        $value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_IDENTITY);
106
107
        assert($value === null || is_string($value));
108
109
        return $value;
110
    }
111
112 1
    /**
113
     * @param ContextInterface $context
114 1
     *
115
     * @return bool
116
     */
117
    protected static function reqHasResourceAttributes(ContextInterface $context): bool
118
    {
119
        return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_ATTRIBUTES);
120
    }
121
122 1
    /**
123
     * @param ContextInterface $context
124 1
     *
125
     * @return string|int|array|null
126 1
     */
127
    protected static function reqGetResourceAttributes(ContextInterface $context): array
128 1
    {
129
        assert(static::reqHasResourceAttributes($context));
130 1
131
        $value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_ATTRIBUTES);
132
133
        assert(is_array($value));
134
135
        return $value;
136
    }
137
138 1
    /**
139
     * @param ContextInterface $context
140 1
     *
141
     * @return bool
142
     */
143
    protected static function reqHasResourceRelationships(ContextInterface $context): bool
144
    {
145
        return $context->getRequest()->has(RequestProperties::REQ_RESOURCE_RELATIONSHIPS);
146
    }
147
148 1
    /**
149
     * @param ContextInterface $context
150 1
     *
151
     * @return string|int|array|null
152 1
     */
153
    protected static function reqGetResourceRelationships(ContextInterface $context): array
154 1
    {
155
        assert(static::reqHasResourceRelationships($context));
156 1
157
        $value = $context->getRequest()->get(RequestProperties::REQ_RESOURCE_RELATIONSHIPS);
158
159
        assert(is_array($value));
160
161
        return $value;
162
    }
163
164
    /**
165
     * @param ContextInterface $context
166
     *
167 1
     * @return bool
168
     *
169
     * @throws ContainerExceptionInterface
170 1
     * @throws NotFoundExceptionInterface
171 1
     */
172 1
    protected static function ctxHasCurrentAccount(ContextInterface $context): bool
173
    {
174 1
        /** @var AccountManagerInterface $manager */
175
        $container = static::ctxGetContainer($context);
176
        $manager   = $container->get(AccountManagerInterface::class);
177
        $account   = $manager->getAccount();
178
179
        return $account !== null;
180
    }
181
182
    /**
183
     * @param ContextInterface $context
184
     *
185 1
     * @return AccountInterface
186
     *
187 1
     * @throws ContainerExceptionInterface
188
     * @throws NotFoundExceptionInterface
189
     */
190 1
    protected static function ctxGetCurrentAccount(ContextInterface $context): AccountInterface
191 1
    {
192 1
        assert(static::ctxHasCurrentAccount($context));
193
194 1
        /** @var AccountManagerInterface $manager */
195
        $container = static::ctxGetContainer($context);
196
        $manager   = $container->get(AccountManagerInterface::class);
197
        $account   = $manager->getAccount();
198
199
        return $account;
200
    }
201
202 1
    /**
203
     * @param ContextInterface $context
204 1
     *
205
     * @return bool
206
     */
207
    protected static function ctxHasContainer(ContextInterface $context): bool
208
    {
209
        return $context->has(ContextProperties::CTX_CONTAINER);
210
    }
211
212 1
    /**
213
     * @param ContextInterface $context
214 1
     *
215
     * @return ContainerInterface
216 1
     */
217
    protected static function ctxGetContainer(ContextInterface $context): ContainerInterface
218
    {
219
        assert(static::ctxHasContainer($context));
220
221
        return $context->get(ContextProperties::CTX_CONTAINER);
222
    }
223
}
224