Passed
Pull Request — master (#38)
by Melech
05:50 queued 01:43
created

ServiceProvider::publishConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
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\Session\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\Cache\Contract\Cache;
18
use Valkyrja\Container\Contract\Container;
19
use Valkyrja\Container\Support\Provider;
20
use Valkyrja\Crypt\Contract\Crypt;
21
use Valkyrja\Http\Message\Request\Contract\ServerRequest;
22
use Valkyrja\Log\Contract\Logger;
23
use Valkyrja\Session\Adapter\CacheAdapter;
24
use Valkyrja\Session\Adapter\Contract\Adapter;
25
use Valkyrja\Session\Adapter\CookieAdapter;
26
use Valkyrja\Session\Adapter\LogAdapter;
27
use Valkyrja\Session\Adapter\NullAdapter;
28
use Valkyrja\Session\Adapter\PHPAdapter;
29
use Valkyrja\Session\Config;
30
use Valkyrja\Session\Config\CacheConfiguration;
31
use Valkyrja\Session\Config\CookieConfiguration;
32
use Valkyrja\Session\Config\LogConfiguration;
33
use Valkyrja\Session\Config\NullConfiguration;
34
use Valkyrja\Session\Config\PhpConfiguration;
35
use Valkyrja\Session\Contract\Session;
36
use Valkyrja\Session\Driver\Driver;
37
use Valkyrja\Session\Factory\ContainerFactory;
38
use Valkyrja\Session\Factory\Contract\Factory;
39
40
/**
41
 * Class ServiceProvider.
42
 *
43
 * @author Melech Mizrachi
44
 */
45
final class ServiceProvider extends Provider
46
{
47
    /**
48
     * @inheritDoc
49
     */
50
    public static function publishers(): array
51
    {
52
        return [
53
            Session::class       => [self::class, 'publishSession'],
54
            Factory::class       => [self::class, 'publishFactory'],
55
            Driver::class        => [self::class, 'publishDriver'],
56
            NullAdapter::class   => [self::class, 'publishNullAdapter'],
57
            CacheAdapter::class  => [self::class, 'publishCacheAdapter'],
58
            CookieAdapter::class => [self::class, 'publishCookieAdapter'],
59
            LogAdapter::class    => [self::class, 'publishLogAdapter'],
60
            PHPAdapter::class    => [self::class, 'publishPHPAdapter'],
61
            Config::class        => [self::class, 'publishConfig'],
62
        ];
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public static function provides(): array
69
    {
70
        return [
71
            Session::class,
72
            Factory::class,
73
            Driver::class,
74
            NullAdapter::class,
75
            CacheAdapter::class,
76
            CookieAdapter::class,
77
            LogAdapter::class,
78
            PHPAdapter::class,
79
            Config::class,
80
        ];
81
    }
82
83
    /**
84
     * Publish the Config service.
85
     */
86
    public static function publishConfig(Container $container): void
87
    {
88
        $env = $container->getSingleton(Env::class);
89
90
        $container->setSingleton(Config::class, Config::fromEnv($env::class));
91
    }
92
93
    /**
94
     * Publish the session service.
95
     */
96
    public static function publishSession(Container $container): void
97
    {
98
        $config = $container->getSingleton(Config::class);
99
100
        $container->setSingleton(
101
            Session::class,
102
            new \Valkyrja\Session\Session(
103
                factory: $container->getSingleton(Factory::class),
104
                config: $config
105
            )
106
        );
107
    }
108
109
    /**
110
     * Publish the factory service.
111
     */
112
    public static function publishFactory(Container $container): void
113
    {
114
        $container->setSingleton(
115
            Factory::class,
116
            new ContainerFactory(container: $container),
117
        );
118
    }
119
120
    /**
121
     * Publish a driver service.
122
     */
123
    public static function publishDriver(Container $container): void
124
    {
125
        $container->setCallable(
126
            Driver::class,
127
            [self::class, 'createDriver']
128
        );
129
    }
130
131
    public static function createDriver(Container $container, Adapter $adapter): Driver
132
    {
133
        return new Driver(
134
            adapter: $adapter
135
        );
136
    }
137
138
    /**
139
     * Publish an adapter service.
140
     */
141
    public static function publishNullAdapter(Container $container): void
142
    {
143
        $container->setCallable(
144
            Adapter::class,
145
            [self::class, 'createNullAdapter']
146
        );
147
    }
148
149
    /**
150
     * Create a null adapter.
151
     */
152
    public static function createNullAdapter(Container $container, NullConfiguration $config): Adapter
153
    {
154
        return new NullAdapter(
155
            config: $config
156
        );
157
    }
158
159
    /**
160
     * Publish a cache adapter service.
161
     */
162
    public static function publishCacheAdapter(Container $container): void
163
    {
164
        $container->setCallable(
165
            CacheAdapter::class,
166
            [self::class, 'createCacheAdapter']
167
        );
168
    }
169
170
    /**
171
     * Create a cache adapter.
172
     */
173
    public static function createCacheAdapter(Container $container, CacheConfiguration $config): CacheAdapter
174
    {
175
        $cache = $container->getSingleton(Cache::class);
176
177
        return new CacheAdapter(
178
            cache: $cache->use($config->cache),
179
            config: $config
180
        );
181
    }
182
183
    /**
184
     * Publish a log adapter service.
185
     *
186
     * @param Container $container The container
187
     *
188
     * @return void
189
     */
190
    public static function publishLogAdapter(Container $container): void
191
    {
192
        $container->setCallable(
193
            LogAdapter::class,
194
            [self::class, 'createLogAdapter']
195
        );
196
    }
197
198
    /**
199
     * Create a log adapter.
200
     */
201
    public static function createLogAdapter(Container $container, LogConfiguration $config): LogAdapter
202
    {
203
        return new LogAdapter(
204
            logger: $container->getSingleton(Logger::class),
205
            config: $config
206
        );
207
    }
208
209
    /**
210
     * Publish the cookie adapter service.
211
     */
212
    public static function publishCookieAdapter(Container $container): void
213
    {
214
        $container->setCallable(
215
            CookieAdapter::class,
216
            [self::class, 'createCookieAdapter']
217
        );
218
    }
219
220
    /**
221
     * Create a cookie adapter.
222
     */
223
    public static function createCookieAdapter(Container $container, CookieConfiguration $config): CookieAdapter
224
    {
225
        $crypt   = $container->getSingleton(Crypt::class);
226
        $request = $container->getSingleton(ServerRequest::class);
227
228
        return new CookieAdapter(
229
            crypt: $crypt,
230
            request: $request,
231
            config: $config
232
        );
233
    }
234
235
    /**
236
     * Publish a php adapter service.
237
     */
238
    public static function publishPhpAdapter(Container $container): void
239
    {
240
        $container->setCallable(
241
            Adapter::class,
242
            [self::class, 'createPhpAdapter']
243
        );
244
    }
245
246
    /**
247
     * Create a php adapter.
248
     */
249
    public static function createPhpAdapter(Container $container, PhpConfiguration $config): Adapter
250
    {
251
        return new PHPAdapter(
252
            config: $config
253
        );
254
    }
255
}
256