Completed
Push — develop ( 1bc5bd...8e829c )
by Neomerx
02:13
created

AuthorizationSettings   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
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 62
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 namespace Limoncello\Application\Packages\Authorization;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Application\Authorization\AuthorizationRulesLoader;
20
use Limoncello\Contracts\Settings\SettingsInterface;
21
22
/**
23
 * @package Limoncello\Application
24
 */
25
abstract class AuthorizationSettings implements SettingsInterface
26
{
27
    /** Settings key */
28
    const KEY_LOG_IS_ENABLED = 0;
29
30
    /**
31
     * Settings key
32
     *
33
     * Top level policy set name (used in logging).
34
     */
35
    const KEY_TOP_POLICY_NAME = self::KEY_LOG_IS_ENABLED + 1;
36
37
    /** Settings key */
38
    const KEY_POLICIES_FOLDER = self::KEY_TOP_POLICY_NAME + 1;
39
40
    /** Settings key */
41
    const KEY_POLICIES_FILE_MASK = self::KEY_POLICIES_FOLDER + 1;
42
43
    /** Settings key */
44
    const KEY_POLICIES_DATA = self::KEY_POLICIES_FILE_MASK + 1;
45
46
    /** Settings key */
47
    const KEY_LAST = self::KEY_POLICIES_DATA;
48
49
    /**
50
     * @inheritdoc
51
     */
52 2
    final public function get(): array
53
    {
54 2
        $defaults = $this->getSettings();
55
56 2
        $policiesFolder   = $defaults[static::KEY_POLICIES_FOLDER] ?? null;
57 2
        $policiesFileMask = $defaults[static::KEY_POLICIES_FILE_MASK] ?? null;
58
59 2
        assert(
60 2
            $policiesFolder !== null && empty(glob($policiesFolder)) === false,
61 2
            "Invalid Policies folder `$policiesFolder`."
62
        );
63 2
        assert(empty($policiesFileMask) === false, "Invalid Policies file mask `$policiesFileMask`.");
64 2
        $policiesPath = $policiesFolder . DIRECTORY_SEPARATOR . $policiesFileMask;
65
66 2
        $topPolicyName = $defaults[static::KEY_TOP_POLICY_NAME];
67
68 2
        $loader = (new AuthorizationRulesLoader($policiesPath, $topPolicyName));
69
70
        return $defaults + [
71 2
                static::KEY_POLICIES_DATA => $loader->getRulesData(),
72
            ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78 2
    protected function getSettings(): array
79
    {
80
        return [
81 2
            static::KEY_LOG_IS_ENABLED     => true,
82 2
            static::KEY_TOP_POLICY_NAME    => 'Application',
83 2
            static::KEY_POLICIES_FILE_MASK => '*.php',
84
        ];
85
    }
86
}
87