Passed
Push — master ( 696cbf...968eef )
by Melech
05:53 queued 01:56
created

ServiceProvider::publishFirebaseJwt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 28
rs 9.6666
c 1
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\Jwt\Provider;
15
16
use OpenSSLAsymmetricKey;
17
use OpenSSLCertificate;
18
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...
19
use Valkyrja\Container\Contract\Container;
20
use Valkyrja\Container\Support\Provider;
21
use Valkyrja\Jwt\Contract\Jwt;
22
use Valkyrja\Jwt\Enum\Algorithm;
23
use Valkyrja\Jwt\FirebaseJwt;
24
use Valkyrja\Jwt\NullJwt;
25
26
/**
27
 * Class ServiceProvider.
28
 *
29
 * @author Melech Mizrachi
30
 */
31
final class ServiceProvider extends Provider
32
{
33
    /**
34
     * @inheritDoc
35
     */
36
    public static function publishers(): array
37
    {
38
        return [
39
            Jwt::class         => [self::class, 'publishJwt'],
40
            FirebaseJwt::class => [self::class, 'publishFirebaseJwt'],
41
            NullJwt::class     => [self::class, 'publishNullJwt'],
42
        ];
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public static function provides(): array
49
    {
50
        return [
51
            Jwt::class,
52
            FirebaseJwt::class,
53
            NullJwt::class,
54
        ];
55
    }
56
57
    /**
58
     * Publish the jwt service.
59
     */
60
    public static function publishJwt(Container $container): void
61
    {
62
        $container->setSingleton(
63
            Jwt::class,
64
            $container->getSingleton(FirebaseJwt::class),
65
        );
66
    }
67
68
    /**
69
     * Publish the jwt service.
70
     */
71
    public static function publishFirebaseJwt(Container $container): void
72
    {
73
        $env = $container->getSingleton(Env::class);
74
        /** @var Algorithm $algorithm */
75
        $algorithm = $env::JWT_ALGORITHM;
76
77
        /** @var OpenSSLAsymmetricKey|OpenSSLCertificate|string $encodeKey */
78
        $encodeKey = match ($algorithm) {
79
            Algorithm::HS256, Algorithm::HS384, Algorithm::HS512 => $env::JWT_HS_KEY,
80
            Algorithm::RS256, Algorithm::RS384, Algorithm::RS512 => $env::JWT_RS_PRIVATE_KEY,
81
            Algorithm::EdDSA => $env::JWT_EDDSA_PRIVATE_KEY,
82
            default          => $env::APP_KEY,
83
        };
84
85
        /** @var OpenSSLAsymmetricKey|OpenSSLCertificate|string $decodeKey */
86
        $decodeKey = match ($algorithm) {
87
            Algorithm::HS256, Algorithm::HS384, Algorithm::HS512 => $env::JWT_HS_KEY,
88
            Algorithm::RS256, Algorithm::RS384, Algorithm::RS512 => $env::JWT_RS_PUBLIC_KEY,
89
            Algorithm::EdDSA => $env::JWT_EDDSA_PUBLIC_KEY,
90
            default          => $env::APP_KEY,
91
        };
92
93
        $container->setSingleton(
94
            FirebaseJwt::class,
95
            new FirebaseJwt(
96
                encodeKey: $encodeKey,
97
                decodeKey: $decodeKey,
98
                algorithm: $algorithm,
99
            ),
100
        );
101
    }
102
103
    /**
104
     * Publish the jwt service.
105
     */
106
    public static function publishNullJwt(Container $container): void
107
    {
108
        $container->setSingleton(
109
            NullJwt::class,
110
            new NullJwt(),
111
        );
112
    }
113
}
114