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\Filesystem\Provider;
15
16
use Aws\S3\S3Client as AwsS3Client;
17
use League\Flysystem\AwsS3V3\AwsS3V3Adapter as FlysystemAwsS3Adapter;
18
use League\Flysystem\Filesystem as Flysystem;
19
use League\Flysystem\Local\LocalFilesystemAdapter as FlysystemLocalAdapter;
20
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...
21
use Valkyrja\Container\Contract\Container;
22
use Valkyrja\Container\Support\Provider;
23
use Valkyrja\Filesystem\Adapter\Contract\Adapter;
24
use Valkyrja\Filesystem\Adapter\FlysystemAdapter;
25
use Valkyrja\Filesystem\Adapter\InMemoryAdapter;
26
use Valkyrja\Filesystem\Config;
27
use Valkyrja\Filesystem\Config\FlysystemConfiguration;
28
use Valkyrja\Filesystem\Config\InMemoryConfiguration;
29
use Valkyrja\Filesystem\Config\LocalFlysystemConfiguration;
30
use Valkyrja\Filesystem\Config\S3FlysystemConfiguration;
31
use Valkyrja\Filesystem\Contract\Filesystem;
32
use Valkyrja\Filesystem\Driver\Driver;
33
use Valkyrja\Filesystem\Factory\ContainerFactory;
34
use Valkyrja\Filesystem\Factory\Contract\Factory;
35
36
/**
37
 * Class ServiceProvider.
38
 *
39
 * @author Melech Mizrachi
40
 */
41
final class ServiceProvider extends Provider
42
{
43
    /**
44
     * @inheritDoc
45
     */
46
    public static function publishers(): array
47
    {
48
        return [
49
            Filesystem::class            => [self::class, 'publishFilesystem'],
50
            Factory::class               => [self::class, 'publishFactory'],
51
            Driver::class                => [self::class, 'publishDriver'],
52
            FlysystemAdapter::class      => [self::class, 'publishFlysystemAdapter'],
53
            FlysystemLocalAdapter::class => [self::class, 'publishFlysystemLocalAdapter'],
54
            FlysystemAwsS3Adapter::class => [self::class, 'publishFlysystemAwsS3Adapter'],
55
            InMemoryAdapter::class       => [self::class, 'publishInMemoryAdapter'],
56
            Config::class                => [self::class, 'publishConfig'],
57
        ];
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public static function provides(): array
64
    {
65
        return [
66
            Filesystem::class,
67
            Factory::class,
68
            Driver::class,
69
            FlysystemAdapter::class,
70
            FlysystemLocalAdapter::class,
71
            FlysystemAwsS3Adapter::class,
72
            InMemoryAdapter::class,
73
            Config::class,
74
        ];
75
    }
76
77
    /**
78
     * Publish the Config service.
79
     */
80
    public static function publishConfig(Container $container): void
81
    {
82
        $env = $container->getSingleton(Env::class);
83
84
        $container->setSingleton(Config::class, Config::fromEnv($env::class));
85
    }
86
87
    /**
88
     * Publish the filesystem service.
89
     */
90
    public static function publishFilesystem(Container $container): void
91
    {
92
        $config = $container->getSingleton(Config::class);
93
94
        $container->setSingleton(
95
            Filesystem::class,
96
            new \Valkyrja\Filesystem\Filesystem(
97
                $container->getSingleton(Factory::class),
98
                $config
99
            )
100
        );
101
    }
102
103
    /**
104
     * Publish the factory service.
105
     */
106
    public static function publishFactory(Container $container): void
107
    {
108
        $container->setSingleton(
109
            Factory::class,
110
            new ContainerFactory($container),
111
        );
112
    }
113
114
    /**
115
     * Publish a driver service.
116
     */
117
    public static function publishDriver(Container $container): void
118
    {
119
        $container->setCallable(
120
            Driver::class,
121
            [self::class, 'createDriver']
122
        );
123
    }
124
125
    /**
126
     * Create the driver class.
127
     */
128
    public static function createDriver(Container $container, Adapter $adapter): Driver
129
    {
130
        return new Driver(
131
            $adapter
132
        );
133
    }
134
135
    /**
136
     * Publish the in memory adapter service.
137
     */
138
    public static function publishInMemoryAdapter(Container $container): void
139
    {
140
        $container->setCallable(
141
            InMemoryAdapter::class,
142
            [self::class, 'createInMemoryAdapter']
143
        );
144
    }
145
146
    /**
147
     * Create the in memory adapter.
148
     */
149
    public static function createInMemoryAdapter(Container $container, InMemoryConfiguration $config): InMemoryAdapter
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

149
    public static function createInMemoryAdapter(/** @scrutinizer ignore-unused */ Container $container, InMemoryConfiguration $config): InMemoryAdapter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

149
    public static function createInMemoryAdapter(Container $container, /** @scrutinizer ignore-unused */ InMemoryConfiguration $config): InMemoryAdapter

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
    {
151
        return new InMemoryAdapter();
152
    }
153
154
    /**
155
     * Publish the flysystem adapter service.
156
     */
157
    public static function publishFlysystemAdapter(Container $container): void
158
    {
159
        $container->setCallable(
160
            FlysystemAdapter::class,
161
            [self::class, 'createFlysystemAdapter']
162
        );
163
    }
164
165
    /**
166
     * Create the flysystem adapter.
167
     */
168
    public static function createFlysystemAdapter(Container $container, FlysystemConfiguration $config): FlysystemAdapter
169
    {
170
        return new FlysystemAdapter(
171
            new Flysystem(
172
                $container->get($config->flysystemAdapter, [$config])
173
            )
174
        );
175
    }
176
177
    /**
178
     * Publish the flysystem local adapter service.
179
     */
180
    public static function publishFlysystemLocalAdapter(Container $container): void
181
    {
182
        $container->setCallable(
183
            FlysystemLocalAdapter::class,
184
            [self::class, 'createFlysystemLocalAdapter']
185
        );
186
    }
187
188
    /**
189
     * Create the flysystem local adapter.
190
     */
191
    public static function createFlysystemLocalAdapter(Container $container, LocalFlysystemConfiguration $config): FlysystemLocalAdapter
192
    {
193
        return new FlysystemLocalAdapter(
194
            $config->dir
195
        );
196
    }
197
198
    /**
199
     * Publish the flysystem s3 adapter service.
200
     */
201
    public static function publishFlysystemAwsS3Adapter(Container $container): void
202
    {
203
        $container->setCallable(
204
            FlysystemAwsS3Adapter::class,
205
            [self::class, 'createFlysystemAwsS3Adapter']
206
        );
207
    }
208
209
    /**
210
     * Create the flysystem s3 adapter.
211
     */
212
    public static function createFlysystemAwsS3Adapter(Container $container, S3FlysystemConfiguration $config): FlysystemAwsS3Adapter
213
    {
214
        $clientConfig = [
215
            'credentials' => [
216
                'key'    => $config->key,
217
                'secret' => $config->secret,
218
            ],
219
            'region'      => $config->region,
220
            'version'     => $config->version,
221
        ];
222
223
        return new FlysystemAwsS3Adapter(
224
            client: new AwsS3Client($clientConfig),
225
            bucket: $config->bucket,
226
            prefix: $config->prefix,
227
            options: $config->options
228
        );
229
    }
230
}
231