Passed
Push — master ( 1efd90...6b1ebf )
by Melech
03:59
created

ServiceProvider::publishDriver()   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\Cache\Provider;
15
16
use Predis\Client;
17
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...
18
use Valkyrja\Cache\Contract\Cache;
19
use Valkyrja\Cache\LogCache;
20
use Valkyrja\Cache\NullCache;
21
use Valkyrja\Cache\RedisCache;
22
use Valkyrja\Container\Contract\Container;
23
use Valkyrja\Container\Support\Provider;
24
use Valkyrja\Log\Contract\Logger;
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
            Cache::class      => [self::class, 'publishCache'],
40
            RedisCache::class => [self::class, 'publishRedisCache'],
41
            Client::class     => [self::class, 'publishRedisClient'],
42
            LogCache::class   => [self::class, 'publishLogCache'],
43
            NullCache::class  => [self::class, 'publishNullCache'],
44
        ];
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public static function provides(): array
51
    {
52
        return [
53
            Cache::class,
54
            RedisCache::class,
55
            Client::class,
56
            LogCache::class,
57
            NullCache::class,
58
        ];
59
    }
60
61
    /**
62
     * Publish the cache service.
63
     */
64
    public static function publishCache(Container $container): void
65
    {
66
        $container->setSingleton(
67
            Cache::class,
68
            $container->getSingleton(RedisCache::class)
69
        );
70
    }
71
72
    /**
73
     * Publish the redis cache service.
74
     */
75
    public static function publishRedisCache(Container $container): void
76
    {
77
        $env = $container->getSingleton(Env::class);
78
        /** @var string $prefix */
79
        $prefix = $env::CACHE_REDIS_PREFIX;
80
81
        $container->setSingleton(
82
            RedisCache::class,
83
            new RedisCache(
84
                client: $container->getSingleton(Client::class),
85
                prefix: $prefix
86
            )
87
        );
88
    }
89
90
    /**
91
     * Publish the redis client service.
92
     */
93
    public static function publishRedisClient(Container $container): void
94
    {
95
        $env = $container->getSingleton(Env::class);
96
        /** @var non-empty-string $host */
97
        $host = $env::CACHE_REDIS_HOST;
98
        /** @var int $port */
99
        $port = $env::CACHE_REDIS_PORT;
100
101
        $container->setSingleton(
102
            Client::class,
103
            new Client(
104
                parameters: [
105
                    'host' => $host,
106
                    'port' => $port,
107
                ]
108
            )
109
        );
110
    }
111
112
    /**
113
     * Publish the log cache service.
114
     */
115
    public static function publishLogCache(Container $container): void
116
    {
117
        $env = $container->getSingleton(Env::class);
118
        /** @var string $prefix */
119
        $prefix = $env::CACHE_LOG_PREFIX;
120
        /** @var class-string<Logger> $logger */
121
        $logger = $env::CACHE_LOG_LOGGER;
122
123
        $container->setSingleton(
124
            LogCache::class,
125
            new LogCache(
126
                logger: $container->get($logger),
127
                prefix: $prefix
128
            )
129
        );
130
    }
131
132
    /**
133
     * Publish the null cache service.
134
     */
135
    public static function publishNullCache(Container $container): void
136
    {
137
        $env = $container->getSingleton(Env::class);
138
        /** @var string $prefix */
139
        $prefix = $env::CACHE_NULL_PREFIX;
140
141
        $container->setSingleton(
142
            NullCache::class,
143
            new NullCache(
144
                prefix: $prefix
145
            )
146
        );
147
    }
148
}
149