AuthorizationSettings   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 22 2
A getSettings() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Packages\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\Application\Authorization\AuthorizationRulesLoader;
22
use Limoncello\Contracts\Settings\Packages\AuthorizationSettingsInterface;
23
use ReflectionException;
24
use function assert;
25
use function glob;
26
27
/**
28
 * @package Limoncello\Application
29
 */
30
abstract class AuthorizationSettings implements AuthorizationSettingsInterface
31
{
32
    /**
33 2
     * @inheritdoc
34
     *
35 2
     * @throws ReflectionException
36
     */
37 2
    final public function get(array $appConfig): array
38 2
    {
39
        $defaults = $this->getSettings();
40 2
41 2
        $policiesFolder   = $defaults[static::KEY_POLICIES_FOLDER] ?? null;
42 2
        $policiesFileMask = $defaults[static::KEY_POLICIES_FILE_MASK] ?? null;
43
44 2
        assert(
45 2
            $policiesFolder !== null && empty(glob($policiesFolder)) === false,
46
            "Invalid Policies folder `$policiesFolder`."
47 2
        );
48
        assert(empty($policiesFileMask) === false, "Invalid Policies file mask `$policiesFileMask`.");
49 2
        $policiesPath = $policiesFolder . DIRECTORY_SEPARATOR . $policiesFileMask;
50
51
        $topPolicyName = $defaults[static::KEY_TOP_POLICY_NAME];
52 2
53
        $loader = (new AuthorizationRulesLoader($policiesPath, $topPolicyName));
54
55
        return $defaults + [
56
                static::KEY_POLICIES_DATA => $loader->getRulesData(),
57
            ];
58
    }
59 2
60
    /**
61
     * @return array
62 2
     */
63 2
    protected function getSettings(): array
64 2
    {
65
        return [
66
            static::KEY_LOG_IS_ENABLED     => true,
67
            static::KEY_TOP_POLICY_NAME    => 'Application',
68
            static::KEY_POLICIES_FILE_MASK => '*.php',
69
        ];
70
    }
71
}
72