Completed
Push — develop ( ea9a36...1bc5bd )
by Neomerx
05:01
created

AuthorizationSettings::getSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
ccs 0
cts 0
cp 0
cc 1
eloc 5
nc 1
nop 0
crap 2
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 2
    const KEY_LAST = self::KEY_POLICIES_DATA;
48
49 2
    /**
50
     * @inheritdoc
51
     */
52 2
    final public function get(): array
53 2
    {
54
        $defaults = $this->getSettings();
55
56
        $policiesFolder   = $defaults[static::KEY_POLICIES_FOLDER] ?? null;
57
        $policiesFileMask = $defaults[static::KEY_POLICIES_FILE_MASK] ?? null;
58
59
        assert(
60
            $policiesFolder !== null && empty(glob($policiesFolder)) === false,
61
            "Invalid Policies folder `$policiesFolder`."
62
        );
63
        assert(empty($policiesFileMask) === false, "Invalid Policies file mask `$policiesFileMask`.");
64
        $policiesPath = $policiesFolder . DIRECTORY_SEPARATOR . $policiesFileMask;
65
66
        $topPolicyName = $defaults[static::KEY_TOP_POLICY_NAME];
67
68
        $loader = (new AuthorizationRulesLoader($policiesPath, $topPolicyName));
69
70
        return $defaults + [
71
                static::KEY_POLICIES_DATA => $loader->getRulesData(),
72
            ];
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    protected function getSettings(): array
79
    {
80
        return [
81
            static::KEY_LOG_IS_ENABLED     => true,
82
            static::KEY_TOP_POLICY_NAME    => 'Application',
83
            static::KEY_POLICIES_FILE_MASK => '*.php',
84
        ];
85
    }
86
}
87