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.
Passed
Push — master ( 217541...83405a )
by Louis-Marie
02:28
created

ApplicationConfiguration::findComposerDir()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LM\AuthAbstractor\Implementation;
6
7
use InvalidArgumentException;
8
use LM\AuthAbstractor\Configuration\IApplicationConfiguration;
9
use LM\AuthAbstractor\Model\IMember;
10
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
11
use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
12
use Closure;
13
use Exception;
14
15
/**
16
 * This is a convenience implementation of IApplicationConfiguration.
17
 *
18
 * It removes
19
 * the burden of having to implement (and then instantiate)
20
 * IApplicationConfiguration. It assumes the library is used as a Composer
21
 * dependency. It also uses native PHP sessions. Finally, it does not let you
22
 * define the password complexity settings. If you want more control over these
23
 * options, you will need to provide your own implementation of
24
 * IApplicationConfiguration.
25
 *
26
 * @see \LM\AuthAbstractor\Configuration\IApplicationConfiguration
27
 * @todo Put implementations in a different library?
28
 */
29
class ApplicationConfiguration implements IApplicationConfiguration
30
{
31
    /** @var string */
32
    private $appId;
33
34
    /** @var string */
35
    private $assetBaseUri;
36
37
    /** @var string */
38
    private $customTwigDir;
39
40
    /** @var string */
41
    private $composerDir;
42
43
    /** @var string */
44
    private $libDir;
0 ignored issues
show
introduced by
The private property $libDir is not used, and could be removed.
Loading history...
45
46
    /** @var Closure */
47
    private $memberFinder;
48
49
    /** @var mixed[] */
50
    private $pwdSettings;
0 ignored issues
show
introduced by
The private property $pwdSettings is not used, and could be removed.
Loading history...
51
52
    /** @var TokenStorageInterface */
53
    private $tokenStorage;
54
55
    /** @var Closure */
56
    private $u2fRegistrationFinder;
57
58
    /**
59
     * @api
60
     * @param string $appId The application ID of the application. This is used
61
     * for U2F challenges. The application ID is either:
62
     *  - The ID of the mobile application, if the application is a mobile
63
     * application. The format of it differs on Android and on Apple devices.
64
     *  - The origin of the website (HTTPS protocol + domain name + port).
65
     *  - A link to an HTTPS link listing all the application IDs of the
66
     * application.
67
     * @param string $assetBaseUri The local path in which assets are stored
68
     * (i.e JavaScript files and CSS files).
69
     * @param Closure $memberFinder A callable to find a user from their
70
     * username. It must return null if the user does not exist.
71
     * @param null|string $customTwigDir A custom Twig directory for overriding
72
     * all, or some templates that come with auth-abstractor by default.
73
     */
74
    public function __construct(
75
        string $appId,
76
        string $assetBaseUri,
77
        Closure $memberFinder,
78
        ?string $customTwigDir = null
79
    ) {
80
        $this->appId = $appId;
81
        $this->assetBaseUri = $assetBaseUri;
82
        $this->composerDir = $this->findComposerDir(__DIR__);
83
        $this->customTwigDir = $customTwigDir;
84
        $this->memberFinder = $memberFinder;
85
        $this->tokenStorage = new NativeSessionTokenStorage();
86
        $this->u2fRegistrationFinder = $u2fRegistrationFinder ?? function ($username) {
0 ignored issues
show
Unused Code introduced by
The parameter $username is not used and could be removed. ( Ignorable by Annotation )

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

86
        $this->u2fRegistrationFinder = $u2fRegistrationFinder ?? function (/** @scrutinizer ignore-unused */ $username) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Comprehensibility Best Practice introduced by
The variable $u2fRegistrationFinder seems to never exist and therefore isset should always be false.
Loading history...
87
            return [];
88
        };
89
    }
90
91
    /**
92
     * @todo Move in another class?
93
     */
94
    private function findComposerDir(string $folder): string
95
    {
96
        if (is_dir("{$folder}/vendor") && is_file("{$folder}/composer.json")) {
97
            return "{$folder}/vendor";
98
        }
99
100
        if ('/' === $folder) {
101
            throw new Exception('Composer not found');
102
        }
103
104
        return $this->findComposerDir(realpath("{$folder}/.."));
105
    }
106
107
    public function getAppId(): string
108
    {
109
        return $this->appId;
110
    }
111
112
    public function getAssetUri(string $assetId): string
113
    {
114
        return $this->assetBaseUri.'/'.$assetId;
115
    }
116
117
    public function getComposerDir(): string
118
    {
119
        return $this->composerDir;
120
    }
121
122
    public function getCustomTwigDir(): ?string
123
    {
124
        return $this->customTwigDir;
125
    }
126
127
    public function getLibdir(): string
128
    {
129
        return realpath(__DIR__.'/../../../..');
130
    }
131
132
    /**
133
     * @todo Not efficient.
134
     * @todo What if the state change between isExistingUsername() and the
135
     * second call to $this->memberFinder?
136
     * @todo Exception for non existent member.
137
     */
138
    public function getMember(string $username): IMember
139
    {
140
        if ($this->isExistingMember($username)) {
141
            return ($this->memberFinder)($username);
142
        }
143
        throw new InvalidArgumentException();
144
    }
145
146
    public function getPwdSettings(): array
147
    {
148
        return [
149
            'min_length' => 5,
150
            'enforce_min_length' =>true,
151
            'uppercase' => false,
152
            'special_chars' => false,
153
            'numbers' => false,
154
        ];
155
    }
156
157
    public function getTokenStorage(): TokenStorageInterface
158
    {
159
        return $this->tokenStorage;
160
    }
161
162
    public function getU2fCertificates(): ?array
163
    {
164
        return null;
165
    }
166
167
    public function getU2fRegistrations(string $username): array
168
    {
169
        return ($this->u2fRegistrationFinder)($username);
170
    }
171
172
    public function isExistingMember(string $username): bool
173
    {
174
        return null !== ($this->memberFinder)($username);
175
    }
176
}
177