Completed
Push — master ( 0e7cb7...85d6ec )
by Neomerx
02:27
created

pe()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
1
<?php namespace Limoncello\Passport\Package;
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 Doctrine\DBAL\Connection;
20
use Limoncello\Contracts\Application\ContainerConfiguratorInterface as CCI;
21
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
22
use Limoncello\Contracts\Settings\SettingsProviderInterface;
23
use Limoncello\Passport\Adaptors\MySql\MySqlPassportServerIntegration;
24
use Limoncello\Passport\Adaptors\MySql\TokenRepository;
25
use Limoncello\Passport\Contracts\Entities\DatabaseSchemeInterface;
26
use Limoncello\Passport\Contracts\PassportServerIntegrationInterface;
27
use Limoncello\Passport\Contracts\Repositories\TokenRepositoryInterface;
28
use Limoncello\Passport\Package\PassportSettings as C;
29
use Psr\Container\ContainerInterface as PsrContainerInterface;
30
31
/**
32
 * @package Limoncello\Passport
33
 */
34 View Code Duplication
class MySqlPassportContainerConfigurator extends BasePassportContainerConfigurator implements CCI
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
{
36
    /**
37
     * @inheritdoc
38
     *
39
     * @SuppressWarnings(PHPMD.UnusedLocalVariable)
40
     */
41
    public static function configureContainer(LimoncelloContainerInterface $container)
42
    {
43
        static::baseConfigureContainer($container);
0 ignored issues
show
Unused Code introduced by
The call to the method Limoncello\Passport\Pack...aseConfigureContainer() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
44
45
        $container[PassportServerIntegrationInterface::class] = function (PsrContainerInterface $container) {
46
            assert($container !== null);
47
48
            return new class ($container) extends MySqlPassportServerIntegration
49
            {
50
                /**
51
                 * @var PsrContainerInterface
52
                 */
53
                private $container;
54
55
                /**
56
                 * @var array
57
                 */
58
                private $settings;
59
60
                /**
61
                 * @param PsrContainerInterface $container
62
                 */
63
                public function __construct(PsrContainerInterface $container)
64
                {
65
                    $this->container = $container;
66
                    $this->settings  = $container->get(SettingsProviderInterface::class)->get(C::class);
67
68
                    /** @var Connection $connection */
69
                    $connection = $container->get(Connection::class);
70
71
                    parent::__construct(
72
                        $connection,
73
                        $this->settings[C::KEY_DEFAULT_CLIENT_ID],
74
                        $this->settings[C::KEY_APPROVAL_URI_STRING],
75
                        $this->settings[C::KEY_ERROR_URI_STRING],
76
                        $this->settings[C::KEY_CODE_EXPIRATION_TIME_IN_SECONDS],
77
                        $this->settings[C::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS],
78
                        $this->settings[C::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH]
79
                    );
80
                }
81
82
                /**
83
                 * @inheritdoc
84
                 */
85
                public function validateUserId(string $userName, string $password)
86
                {
87
                    $validator    = $this->settings[C::KEY_USER_CREDENTIALS_VALIDATOR];
88
                    $nullOrUserId = call_user_func($validator, $this->container, $userName, $password);
89
90
                    return $nullOrUserId;
91
                }
92
93
                /**
94
                 * @inheritdoc
95
                 */
96
                public function verifyAllowedUserScope(int $userIdentity, array $scope = null)
97
                {
98
                    $validator   = $this->settings[C::KEY_USER_SCOPE_VALIDATOR];
99
                    $nullOrScope = call_user_func($validator, $this->container, $userIdentity, $scope);
100
101
                    return $nullOrScope;
102
                }
103
            };
104
        };
105
106
        $container[TokenRepositoryInterface::class] = function (PsrContainerInterface $container) {
107
            $connection = $container->get(Connection::class);
108
            $scheme     = $container->get(DatabaseSchemeInterface::class);
109
110
            return new TokenRepository($connection, $scheme);
111
        };
112
    }
113
}
114