PassportSettings::get()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 78
ccs 52
cts 52
cp 1
rs 8.48
c 0
b 0
f 0
cc 4
nc 1
nop 1
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Passport\Package;
4
5
/**
6
 * Copyright 2015-2019 [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\Contracts\Application\ApplicationConfigurationInterface as A;
22
use Limoncello\Contracts\Settings\Packages\PassportSettingsInterface;
23
use Limoncello\Common\Reflection\CheckCallableTrait;
24
use Limoncello\Passport\Contracts\Entities\TokenInterface;
25
use Psr\Container\ContainerInterface;
26
use Psr\Http\Message\ResponseInterface;
27
use ReflectionException;
28
use ReflectionParameter;
29
use function assert;
30
31
/**
32
 * @package Limoncello\Passport
33
 */
34
class PassportSettings implements PassportSettingsInterface
35
{
36
    use CheckCallableTrait;
37
38
    /**
39
     * @var array
40
     */
41
    private $appConfig;
42
43
    /**
44
     * @inheritdoc
45
     *
46
     * @throws ReflectionException
47
     */
48 1
    final public function get(array $appConfig): array
49
    {
50 1
        $this->appConfig = $appConfig;
51
52 1
        $defaults = $this->getSettings();
53
54 1
        $credentialsValidator = $defaults[static::KEY_USER_CREDENTIALS_VALIDATOR];
55 1
        assert(
56 1
            $this->checkPublicStaticCallable(
57 1
                $credentialsValidator,
58 1
                [ContainerInterface::class, 'string', 'string']
59
            ),
60 1
            "Invalid credentials validator."
61
        );
62
63 1
        $scopeValidator = $defaults[static::KEY_USER_SCOPE_VALIDATOR] ?? null;
64 1
        assert(
65 1
            $this->checkPublicStaticCallable(
66 1
                $scopeValidator,
67
                [
68 1
                    ContainerInterface::class,
69 1
                    'int',
70
                    function (ReflectionParameter $parameter) {
71 1
                        return $parameter->allowsNull() === true && $parameter->isArray() === true;
72 1
                    },
73
                ]
74
            ),
75 1
            "Invalid scope validator."
76
        );
77
78 1
        $customPropsProvider = $defaults[static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER] ?? null;
79 1
        assert(
80 1
            $customPropsProvider === null ||
81 1
            $this->checkPublicStaticCallable(
82 1
                $customPropsProvider,
83 1
                [ContainerInterface::class, TokenInterface::class],
84 1
                'array'
85
            ),
86 1
            "Invalid token custom properties provider."
87
        );
88
89 1
        $customUnAuthFactory = $defaults[static::KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY] ?? null;
90 1
        assert(
91 1
            $customUnAuthFactory === null ||
92 1
            $this->checkPublicStaticCallable(
93 1
                $customUnAuthFactory,
94 1
                [],
95 1
                ResponseInterface::class
96
            ),
97 1
            "Invalid custom factory."
98
        );
99
100 1
        $approvalUri = $defaults[static::KEY_APPROVAL_URI_STRING];
101 1
        assert(empty($approvalUri) === false, "Invalid Approval URI `$approvalUri`.");
102
103 1
        $errorUri = $defaults[static::KEY_ERROR_URI_STRING];
104 1
        assert(empty($errorUri) === false, "Invalid Error URI `$errorUri`.");
105
106 1
        $defaultClientId = $defaults[static::KEY_DEFAULT_CLIENT_ID];
107 1
        assert(empty($defaultClientId) === false, "Invalid Default Client ID `$defaultClientId`.");
108
109 1
        $userTable = $defaults[static::KEY_USER_TABLE_NAME];
110 1
        assert(empty($userTable) === false, "Invalid User Table Name `$userTable`.");
111
112 1
        $userPk = $defaults[static::KEY_USER_TABLE_NAME];
113 1
        assert(empty($userPk) === false, "Invalid User Primary Key Name `$userPk`.");
114
115
        return $defaults + [
116 1
                static::KEY_APPROVAL_URI_STRING              => $approvalUri,
117 1
                static::KEY_ERROR_URI_STRING                 => $errorUri,
118 1
                static::KEY_DEFAULT_CLIENT_ID                => $defaultClientId,
119 1
                static::KEY_USER_TABLE_NAME                  => $userTable,
120 1
                static::KEY_USER_PRIMARY_KEY_NAME            => $userPk,
121 1
                static::KEY_USER_CREDENTIALS_VALIDATOR       => $credentialsValidator,
122 1
                static::KEY_USER_SCOPE_VALIDATOR             => $scopeValidator,
123 1
                static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER => $customPropsProvider,
124
            ];
125
    }
126
127
    /**
128
     * @return array
129
     */
130 1
    protected function getSettings(): array
131
    {
132 1
        $appConfig = $this->getAppConfig();
133
134
        return [
135 1
            static::KEY_IS_LOG_ENABLED                       => (bool)($appConfig[A::KEY_IS_LOG_ENABLED] ?? false),
136 1
            static::KEY_CODE_EXPIRATION_TIME_IN_SECONDS      => 10 * 60,
137 1
            static::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS     => 60 * 60,
138 1
            static::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH => true,
139
        ];
140
    }
141
142
    /**
143
     * @return mixed
144
     */
145 1
    protected function getAppConfig()
146
    {
147 1
        return $this->appConfig;
148
    }
149
}
150