Passed
Push — master ( 35232d...8d0b59 )
by Melech
06:16 queued 02:04
created

ServiceProvider::publishFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Crypt\Provider;
15
16
use Valkyrja\Application\Config\ValkyrjaConfig;
17
use Valkyrja\Container\Contract\Container;
18
use Valkyrja\Container\Support\Provider;
19
use Valkyrja\Crypt\Contract\Crypt;
20
use Valkyrja\Crypt\NullCrypt;
21
use Valkyrja\Crypt\SodiumCrypt;
22
23
/**
24
 * Class ServiceProvider.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
final class ServiceProvider extends Provider
29
{
30
    /**
31
     * @inheritDoc
32
     */
33
    public static function publishers(): array
34
    {
35
        return [
36
            Crypt::class       => [self::class, 'publishCrypt'],
37
            SodiumCrypt::class => [self::class, 'publishSodiumCrypt'],
38
            NullCrypt::class   => [self::class, 'publishNullCrypt'],
39
        ];
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public static function provides(): array
46
    {
47
        return [
48
            Crypt::class,
49
            SodiumCrypt::class,
50
            NullCrypt::class,
51
        ];
52
    }
53
54
    /**
55
     * Publish the crypt service.
56
     */
57
    public static function publishCrypt(Container $container): void
58
    {
59
        $container->setSingleton(
60
            Crypt::class,
61
            $container->getSingleton(SodiumCrypt::class),
62
        );
63
    }
64
65
    /**
66
     * Publish the sodium crypt service.
67
     */
68
    public static function publishSodiumCrypt(Container $container): void
69
    {
70
        $config = $container->getSingleton(ValkyrjaConfig::class);
71
72
        $container->setSingleton(
73
            SodiumCrypt::class,
74
            new SodiumCrypt(
75
                $config->app->key
76
            )
77
        );
78
    }
79
80
    /**
81
     * Publish the null crypt service.
82
     */
83
    public static function publishNullCrypt(Container $container): void
84
    {
85
        $container->setSingleton(
86
            NullCrypt::class,
87
            new NullCrypt()
88
        );
89
    }
90
}
91