Passed
Push — master ( d3cc39...f2a84f )
by Melech
03:59
created

ServiceProvider::publishSessionAuthenticator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Auth\Provider;
15
16
use Valkyrja\Application\Env;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Env was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Auth\Contract\Authenticator;
18
use Valkyrja\Auth\EncryptedJwtAuthenticator;
19
use Valkyrja\Auth\EncryptedTokenAuthenticator;
20
use Valkyrja\Auth\Entity\Contract\User;
21
use Valkyrja\Auth\Hasher\Contract\PasswordHasher;
22
use Valkyrja\Auth\JwtAuthenticator;
23
use Valkyrja\Auth\SessionAuthenticator;
24
use Valkyrja\Auth\Store\Contract\Store;
25
use Valkyrja\Auth\Store\InMemoryStore;
26
use Valkyrja\Auth\Store\NullStore;
27
use Valkyrja\Auth\Store\OrmStore;
28
use Valkyrja\Auth\TokenAuthenticator;
29
use Valkyrja\Container\Contract\Container;
30
use Valkyrja\Container\Support\Provider;
31
use Valkyrja\Crypt\Contract\Crypt;
32
use Valkyrja\Http\Message\Request\Contract\ServerRequest;
33
use Valkyrja\Jwt\Contract\Jwt;
34
use Valkyrja\Orm\Contract\Manager;
35
use Valkyrja\Session\Contract\Session;
36
37
/**
38
 * Class ServiceProvider.
39
 *
40
 * @author Melech Mizrachi
41
 */
42
final class ServiceProvider extends Provider
43
{
44
    /**
45
     * @inheritDoc
46
     */
47
    public static function publishers(): array
48
    {
49
        return [
50
            Authenticator::class               => [self::class, 'publishAuthenticator'],
51
            EncryptedJwtAuthenticator::class   => [self::class, 'publishEncryptedJwtAuthenticator'],
52
            EncryptedTokenAuthenticator::class => [self::class, 'publishEncryptedTokenAuthenticator'],
53
            JwtAuthenticator::class            => [self::class, 'publishJwtAuthenticator'],
54
            SessionAuthenticator::class        => [self::class, 'publishSessionAuthenticator'],
55
            TokenAuthenticator::class          => [self::class, 'publishTokenAuthenticator'],
56
            Store::class                       => [self::class, 'publishStore'],
57
            OrmStore::class                    => [self::class, 'publishOrmStore'],
58
            InMemoryStore::class               => [self::class, 'publishInMemoryStore'],
59
            NullStore::class                   => [self::class, 'publishNullStore'],
60
            PasswordHasher::class              => [self::class, 'publishPasswordHasher'],
61
        ];
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public static function provides(): array
68
    {
69
        return [
70
            Authenticator::class,
71
            Store::class,
72
            OrmStore::class,
73
            InMemoryStore::class,
74
            NullStore::class,
75
            PasswordHasher::class,
76
        ];
77
    }
78
79
    /**
80
     * Publish the authenticator service.
81
     */
82
    public static function publishAuthenticator(Container $container): void
83
    {
84
        $env = $container->getSingleton(Env::class);
85
        /** @var class-string<Authenticator> $default */
86
        $default = $env::AUTH_DEFAULT_AUTHENTICATOR;
87
88
        $container->setSingleton(
89
            Authenticator::class,
90
            $container->getSingleton($default),
91
        );
92
    }
93
94
    /**
95
     * Publish the encrypted jwt authenticator service.
96
     */
97
    public static function publishEncryptedJwtAuthenticator(Container $container): void
98
    {
99
        $env = $container->getSingleton(Env::class);
100
        /** @var class-string<User> $entity */
101
        $entity = $env::AUTH_DEFAULT_USER_ENTITY;
102
        /** @var non-empty-string $headerName */
103
        $headerName = $env::AUTH_DEFAULT_AUTHORIZATION_HEADER;
104
105
        $container->setSingleton(
106
            EncryptedJwtAuthenticator::class,
107
            new EncryptedJwtAuthenticator(
108
                crypt: $container->getSingleton(Crypt::class),
109
                jwt: $container->getSingleton(Jwt::class),
110
                request: $container->getSingleton(ServerRequest::class),
111
                store: $container->getSingleton(Store::class),
112
                hasher: $container->getSingleton(PasswordHasher::class),
113
                entity: $entity,
114
                headerName: $headerName,
115
            ),
116
        );
117
    }
118
119
    /**
120
     * Publish the encrypted token authenticator service.
121
     */
122
    public static function publishEncryptedTokenAuthenticator(Container $container): void
123
    {
124
        $env = $container->getSingleton(Env::class);
125
        /** @var class-string<User> $entity */
126
        $entity = $env::AUTH_DEFAULT_USER_ENTITY;
127
        /** @var non-empty-string $headerName */
128
        $headerName = $env::AUTH_DEFAULT_AUTHORIZATION_HEADER;
129
130
        $container->setSingleton(
131
            EncryptedTokenAuthenticator::class,
132
            new EncryptedTokenAuthenticator(
133
                crypt: $container->getSingleton(Crypt::class),
134
                request: $container->getSingleton(ServerRequest::class),
135
                store: $container->getSingleton(Store::class),
136
                hasher: $container->getSingleton(PasswordHasher::class),
137
                entity: $entity,
138
                headerName: $headerName,
139
            ),
140
        );
141
    }
142
143
    /**
144
     * Publish the jwt authenticator service.
145
     */
146
    public static function publishJwtAuthenticator(Container $container): void
147
    {
148
        $env = $container->getSingleton(Env::class);
149
        /** @var class-string<User> $entity */
150
        $entity = $env::AUTH_DEFAULT_USER_ENTITY;
151
        /** @var non-empty-string $headerName */
152
        $headerName = $env::AUTH_DEFAULT_AUTHORIZATION_HEADER;
153
154
        $container->setSingleton(
155
            JwtAuthenticator::class,
156
            new JwtAuthenticator(
157
                jwt: $container->getSingleton(Jwt::class),
158
                request: $container->getSingleton(ServerRequest::class),
159
                store: $container->getSingleton(Store::class),
160
                hasher: $container->getSingleton(PasswordHasher::class),
161
                entity: $entity,
162
                headerName: $headerName,
163
            ),
164
        );
165
    }
166
167
    /**
168
     * Publish the session authenticator service.
169
     */
170
    public static function publishSessionAuthenticator(Container $container): void
171
    {
172
        $env = $container->getSingleton(Env::class);
173
        /** @var class-string<User> $entity */
174
        $entity = $env::AUTH_DEFAULT_USER_ENTITY;
175
        /** @var non-empty-string $sessionId */
176
        $sessionId = $env::AUTH_DEFAULT_SESSION_ID;
177
178
        $container->setSingleton(
179
            SessionAuthenticator::class,
180
            new SessionAuthenticator(
181
                session: $container->getSingleton(Session::class),
182
                store: $container->getSingleton(Store::class),
183
                hasher: $container->getSingleton(PasswordHasher::class),
184
                entity: $entity,
185
                sessionId: $sessionId,
186
            ),
187
        );
188
    }
189
190
    /**
191
     * Publish the token authenticator service.
192
     */
193
    public static function publishTokenAuthenticator(Container $container): void
194
    {
195
        $env = $container->getSingleton(Env::class);
196
        /** @var class-string<User> $entity */
197
        $entity = $env::AUTH_DEFAULT_USER_ENTITY;
198
        /** @var non-empty-string $headerName */
199
        $headerName = $env::AUTH_DEFAULT_AUTHORIZATION_HEADER;
200
201
        $container->setSingleton(
202
            TokenAuthenticator::class,
203
            new TokenAuthenticator(
204
                request: $container->getSingleton(ServerRequest::class),
205
                store: $container->getSingleton(Store::class),
206
                hasher: $container->getSingleton(PasswordHasher::class),
207
                entity: $entity,
208
                headerName: $headerName,
209
            ),
210
        );
211
    }
212
213
    /**
214
     * Publish the store service.
215
     */
216
    public static function publishStore(Container $container): void
217
    {
218
        $env = $container->getSingleton(Env::class);
219
        /** @var class-string<Store> $default */
220
        $default = $env::AUTH_DEFAULT_STORE;
221
222
        $container->setSingleton(
223
            Store::class,
224
            $container->getSingleton($default),
225
        );
226
    }
227
228
    /**
229
     * Publish the orm store service.
230
     */
231
    public static function publishOrmStore(Container $container): void
232
    {
233
        $container->setSingleton(
234
            OrmStore::class,
235
            new OrmStore(
236
                orm: $container->getSingleton(Manager::class)
237
            ),
238
        );
239
    }
240
241
    /**
242
     * Publish the in memory store service.
243
     */
244
    public static function publishInMemoryStore(Container $container): void
245
    {
246
        $container->setSingleton(
247
            InMemoryStore::class,
248
            new InMemoryStore(),
249
        );
250
    }
251
252
    /**
253
     * Publish the null store service.
254
     */
255
    public static function publishNullStore(Container $container): void
256
    {
257
        $container->setSingleton(
258
            NullStore::class,
259
            new NullStore(),
260
        );
261
    }
262
263
    /**
264
     * Publish the password hasher service.
265
     */
266
    public static function publishPasswordHasher(Container $container): void
267
    {
268
        $container->setSingleton(
269
            PasswordHasher::class,
270
            new \Valkyrja\Auth\Hasher\PasswordHasher()
271
        );
272
    }
273
}
274