GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

KernelMocker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 124
rs 10
c 2
b 0
f 0
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
createKernel() 0 104 ?
A hp$0 ➔ getTokenStorage() 0 3 1
A hp$0 ➔ getComposerDir() 0 3 1
A hp$0 ➔ getCustomTwigDir() 0 3 1
A hp$0 ➔ getAppId() 0 3 1
A hp$0 ➔ getLibDir() 0 3 1
A hp$0 ➔ getU2fRegistrations() 0 3 1
A hp$0 ➔ isExistingMember() 0 3 1
B hp$0 ➔ createKernel() 0 104 1
A hp$0 ➔ __construct() 0 13 1
A hp$0 ➔ getU2fCertificates() 0 3 1
A hp$0 ➔ getMember() 0 8 2
A hp$0 ➔ getAssetUri() 0 3 1
A hp$0 ➔ getPwdSettings() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Test;
6
7
use LM\AuthAbstractor\Controller\AuthenticationKernel;
8
use LM\AuthAbstractor\Configuration\IApplicationConfiguration;
9
use LM\AuthAbstractor\Model\IMember;
10
use LM\AuthAbstractor\Model\IAuthenticationKernel;
11
use LM\AuthAbstractor\Mocker\U2fMocker;
12
use LM\AuthAbstractor\Implementation\Member;
13
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
14
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
15
use InvalidArgumentException;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
18
19
/**
20
 * Mocks the kernel.
21
 *
22
 * It only supports one user
23
 *
24
 * @internal
25
 * @todo Maybe it should be moved in tests?
26
 */
27
class KernelMocker
28
{
29
    const KEY_PWD_SETTINGS = 'pwdSettings';
30
31
    const KEY_U2F_CERTIFICATES = 'u2fCertificates';
32
33
    const KEY_USER_ID = 'userId';
34
35
    const KEY_USER_PWD = 'userPwd';
36
37
    const USER_ID = 'user';
38
39
    const USER_PWD = 'pwd';
40
41
    /**
42
     * @param array $options An array of options for initialising the kernel.
43
     * The available options are the constants of KernelMocker that begin with
44
     * KEY_.
45
     * @return IAuthenticationKernel A kernel that can be used for unit testing.
46
     */
47
    public function createKernel(array $options = []): IAuthenticationKernel
48
    {
49
        if (!isset($options[self::KEY_U2F_CERTIFICATES])) {
50
            $options[self::KEY_U2F_CERTIFICATES] = null;
51
        }
52
        if (!isset($options[self::KEY_PWD_SETTINGS])) {
53
            $options[self::KEY_PWD_SETTINGS] = [
54
                'min_length' => 0,
55
                'enforce_min_length' => false,
56
                'uppercase' => false,
57
                'special_chars' => false,
58
                'numbers' => false,
59
            ];
60
        }
61
        if (!isset($options[self::KEY_USER_ID])) {
62
            $options[self::KEY_USER_ID] = self::USER_ID;
63
        }
64
        if (!isset($options[self::KEY_USER_PWD])) {
65
            $options[self::KEY_USER_PWD] = self::USER_PWD;
66
        }
67
        return new AuthenticationKernel(
68
            new class($options[self::KEY_U2F_CERTIFICATES], $options[self::KEY_PWD_SETTINGS], $options[self::KEY_USER_ID], $options[self::KEY_USER_PWD]) implements IApplicationConfiguration {
69
                private $cas;
70
71
                private $hashedPassword;
72
73
                private $tokenStorage;
74
            
75
                private $username;
76
77
                public function __construct(
78
                    ?array $cas,
79
                    array $pwdSettings,
80
                    string $username,
81
                    string $password
82
                ) {
83
                    $this->cas = $cas;
84
                    $this->hashedPassword = password_hash($password, PASSWORD_DEFAULT);
85
                    $this->pwdSettings = $pwdSettings;
0 ignored issues
show
Bug Best Practice introduced by
The property pwdSettings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
                    $this->tokenStorage = new SessionTokenStorage(new Session(
87
                        new MockArraySessionStorage()
88
                    ));
89
                    $this->username = $username;
90
                }
91
92
                public function getAssetUri(string $assetId): string
93
                {
94
                    return 'https://example.org';
95
                }
96
97
                public function getAppId(): string
98
                {
99
                    return 'https://example.org';
100
                }
101
102
                public function getComposerDir(): string
103
                {
104
                    return realpath(__DIR__.'/../../../../vendor');
105
                }
106
107
                public function getCustomTwigDir(): ?string
108
                {
109
                    return null;
110
                }
111
112
                public function getLibDir(): string
113
                {
114
                    return realpath(__DIR__.'/../../../..');
115
                }
116
117
                public function getMember(string $username): IMember
118
                {
119
                    if ($this->username !== $username) {
120
                        throw new InvalidArgumentException();
121
                    }
122
                    return new Member(
123
                        $this->hashedPassword,
0 ignored issues
show
Bug introduced by
It seems like $this->hashedPassword can also be of type null; however, parameter $hashedPassword of LM\AuthAbstractor\Implem...n\Member::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                        /** @scrutinizer ignore-type */ $this->hashedPassword,
Loading history...
124
                        $this->username
125
                    );
126
                }
127
128
                public function getU2fCertificates(): ?array
129
                {
130
                    return $this->cas;
131
                }
132
133
                public function getU2fRegistrations(string $username): array
134
                {
135
                    return (new U2fMocker($this))->getU2fRegistrationsOnly();
136
                }
137
138
                public function getPwdSettings(): array
139
                {
140
                    return $this->pwdSettings;
141
                }
142
143
                public function getTokenStorage(): TokenStorageInterface
144
                {
145
                    return $this->tokenStorage;
146
                }
147
148
                public function isExistingMember(string $username): bool
149
                {
150
                    return $this->username === $username;
151
                }
152
            }
153
        );
154
    }
155
}
156