BaseImpersonationMiddleware::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Commands;
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 Closure;
22
use Limoncello\Contracts\Authentication\AccountManagerInterface;
23
use Limoncello\Contracts\Commands\IoInterface;
24
use Limoncello\Contracts\Commands\MiddlewareInterface;
25
use Limoncello\Contracts\Passport\PassportAccountInterface;
26
use Limoncello\Contracts\Settings\Packages\CommandSettingsInterface;
27
use Limoncello\Contracts\Settings\SettingsProviderInterface;
28
use Psr\Container\ContainerInterface;
29
use function array_key_exists;
30
use function assert;
31
use function call_user_func;
32
use function is_int;
33
use function is_string;
34
35
/**
36
 * @package Limoncello\Application
37
 */
38
abstract class BaseImpersonationMiddleware implements MiddlewareInterface
39
{
40
    /**
41
     * @param ContainerInterface $container
42
     *
43 1
     * @return Closure
44
     */
45
    abstract protected static function createReadScopesClosure(ContainerInterface $container): Closure;
46
47
    /**
48
     * @inheritdoc
49 1
     */
50 1
    public static function handle(
51 1
        IoInterface $inOut,
52 1
        Closure $next,
53
        ContainerInterface $container
54
    ): void {
55 1
        /** @var SettingsProviderInterface $provider */
56 1
        $provider       = $container->get(SettingsProviderInterface::class);
57 1
        $settings       = $provider->get(CommandSettingsInterface::class);
58
        $userIdentity   = $settings[CommandSettingsInterface::KEY_IMPERSONATE_AS_USER_IDENTITY] ?? null;
59
        $userProperties = $settings[CommandSettingsInterface::KEY_IMPERSONATE_WITH_USER_PROPERTIES] ?? [];
60 1
61
        /** @var AccountManagerInterface $manager */
62
        $manager = $container->get(AccountManagerInterface::class);
63
        $manager->setAccount(
64
            static::createCliPassport($userIdentity, static::createReadScopesClosure($container), $userProperties)
65
        );
66
67
        call_user_func($next, $inOut);
68
    }
69
70
    /**
71
     * @param int|string $userIdentity
72
     * @param Closure    $readUserScopes
73
     * @param array      $properties
74
     *
75
     * @return PassportAccountInterface
76
     *
77
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
78
     * @SuppressWarnings(PHPMD.UndefinedVariable)
79
     */
80
    protected static function createCliPassport(
81
        $userIdentity,
82
        Closure $readUserScopes,
83
        array $properties
84
    ): PassportAccountInterface {
85
        return new class ($userIdentity, $readUserScopes, $properties) implements PassportAccountInterface
86
        {
87
            /**
88
             * @var array
89
             */
90
            private $properties;
91
92
            /**
93
             * @var int
94
             */
95
            private $userIdentity;
96
97
            /**
98
             * @var Closure
99 1
             */
100
            private $readUserScopes;
101 1
102
            /**
103 1
             * @param int|string $userIdentity
104 1
             * @param Closure    $readUserScopes
105 1
             * @param array      $properties
106
             */
107
            public function __construct($userIdentity, Closure $readUserScopes, array $properties)
108
            {
109
                assert(is_int($userIdentity) === true || is_string($userIdentity) === true);
110
111 1
                $this->userIdentity   = $userIdentity;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userIdentity can also be of type string. However, the property $userIdentity is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
                $this->properties     = $properties;
113 1
                $this->readUserScopes = $readUserScopes;
114
            }
115
116
            /**
117
             * @inheritdoc
118
             */
119 1
            public function hasProperty($key): bool
120
            {
121 1
                return array_key_exists($key, $this->properties);
122
            }
123
124
            /**
125
             * @inheritdoc
126
             */
127 1
            public function getProperty($key)
128
            {
129 1
                return $this->properties[$key];
130
            }
131
132
            /**
133
             * @inheritdoc
134
             */
135 1
            public function hasUserIdentity(): bool
136
            {
137 1
                return true;
138
            }
139
140
            /**
141
             * @inheritdoc
142
             */
143 1
            public function getUserIdentity()
144
            {
145 1
                return $this->userIdentity;
146
            }
147
148
            /**
149
             * @inheritdoc
150
             */
151 1
            public function hasClientIdentity(): bool
152
            {
153 1
                return false;
154
            }
155
156
            /**
157
             * @inheritdoc
158
             */
159 1
            public function getClientIdentity()
160
            {
161
                return null;
162 1
            }
163
164
            /**
165
             * @inheritdoc
166
             */
167
            public function hasScope(string $scope): bool
168 1
            {
169
                // we typically do just one call during a session so it's fine to work with unsorted data.
170 1
                return in_array($scope, $this->getScopes());
171
            }
172
173
            /**
174
             * @inheritdoc
175
             */
176 1
            public function hasScopes(): bool
177
            {
178 1
                return true;
179
            }
180 1
181
            /**
182
             * @inheritdoc
183
             */
184
            public function getScopes(): array
185
            {
186
                $scopes = call_user_func($this->readUserScopes, $this->getUserIdentity());
187
188
                return $scopes;
189
            }
190
        };
191
    }
192
}
193