Completed
Push — master ( 072b77...4ad977 )
by Neomerx
05:47
created

createReadScopesClosure()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php namespace Limoncello\Application\Commands;
2
3
/**
4
 * Copyright 2015-2018 [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 Closure;
20
use Limoncello\Contracts\Authentication\AccountManagerInterface;
21
use Limoncello\Contracts\Commands\IoInterface;
22
use Limoncello\Contracts\Commands\MiddlewareInterface;
23
use Limoncello\Contracts\Passport\PassportAccountInterface;
24
use Limoncello\Contracts\Settings\Packages\CommandSettingsInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Psr\Container\ContainerInterface;
27
28
/**
29
 * @package Limoncello\Application
30
 */
31
abstract class BaseImpersonationMiddleware implements MiddlewareInterface
32
{
33
    /**
34
     * @param ContainerInterface $container
35
     *
36
     * @return Closure
37
     */
38
    abstract protected static function createReadScopesClosure(ContainerInterface $container): Closure;
39
40
    /**
41
     * @inheritdoc
42
     */
43 1
    public static function handle(
44
        IoInterface $inOut,
45
        Closure $next,
46
        ContainerInterface $container
47
    ): void {
48
        /** @var SettingsProviderInterface $provider */
49 1
        $provider       = $container->get(SettingsProviderInterface::class);
50 1
        $settings       = $provider->get(CommandSettingsInterface::class);
51 1
        $userIdentity   = $settings[CommandSettingsInterface::KEY_IMPERSONATE_AS_USER_IDENTITY] ?? null;
52 1
        $userProperties = $settings[CommandSettingsInterface::KEY_IMPERSONATE_WITH_USER_PROPERTIES] ?? [];
53
54
        /** @var AccountManagerInterface $manager */
55 1
        $manager = $container->get(AccountManagerInterface::class);
56 1
        $manager->setAccount(
57 1
            static::createCliPassport($userIdentity, static::createReadScopesClosure($container), $userProperties)
58
        );
59
60 1
        call_user_func($next, $inOut);
61
    }
62
63
    /**
64
     * @param int|string $userIdentity
65
     * @param Closure    $readUserScopes
66
     * @param array      $properties
67
     *
68
     * @return PassportAccountInterface
69
     *
70
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
71
     */
72
    protected static function createCliPassport(
73
        $userIdentity,
74
        Closure $readUserScopes,
75
        array $properties
76
    ): PassportAccountInterface {
77
        return new class ($userIdentity, $readUserScopes, $properties) implements PassportAccountInterface
78
        {
79
            /**
80
             * @var array
81
             */
82
            private $properties;
83
84
            /**
85
             * @var int
86
             */
87
            private $userIdentity;
88
89
            /**
90
             * @var Closure
91
             */
92
            private $readUserScopes;
93
94
            /**
95
             * @param int|string $userIdentity
96
             * @param Closure    $readUserScopes
97
             * @param array      $properties
98
             */
99 1
            public function __construct($userIdentity, Closure $readUserScopes, array $properties)
100
            {
101 1
                assert(is_int($userIdentity) === true || is_string($userIdentity) === true);
102
103 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...
104 1
                $this->properties     = $properties;
105 1
                $this->readUserScopes = $readUserScopes;
106
            }
107
108
            /**
109
             * @inheritdoc
110
             */
111 1
            public function hasProperty($key): bool
112
            {
113 1
                return array_key_exists($key, $this->properties);
114
            }
115
116
            /**
117
             * @inheritdoc
118
             */
119 1
            public function getProperty($key)
120
            {
121 1
                return $this->properties[$key];
122
            }
123
124
            /**
125
             * @inheritdoc
126
             */
127 1
            public function hasUserIdentity(): bool
128
            {
129 1
                return true;
130
            }
131
132
            /**
133
             * @inheritdoc
134
             */
135 1
            public function getUserIdentity()
136
            {
137 1
                return $this->userIdentity;
138
            }
139
140
            /**
141
             * @inheritdoc
142
             */
143 1
            public function hasClientIdentity(): bool
144
            {
145 1
                return false;
146
            }
147
148
            /**
149
             * @inheritdoc
150
             */
151 1
            public function getClientIdentity()
152
            {
153 1
                return null;
154
            }
155
156
            /**
157
             * @inheritdoc
158
             */
159 1
            public function hasScope(string $scope): bool
160
            {
161
                // we typically do just one call during a session so it's fine to work with unsorted data.
162 1
                return in_array($scope, $this->getScopes());
163
            }
164
165
            /**
166
             * @inheritdoc
167
             */
168 1
            public function hasScopes(): bool
169
            {
170 1
                return true;
171
            }
172
173
            /**
174
             * @inheritdoc
175
             */
176 1
            public function getScopes(): array
177
            {
178 1
                $scopes = call_user_func($this->readUserScopes, $this->getUserIdentity());
179
180 1
                return $scopes;
181
            }
182
        };
183
    }
184
}
185