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

ServiceProvider::createFlysystemAwsS3Adapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Contract\Filesystem;
24
use Valkyrja\Filesystem\FlysystemFilesystem;
25
use Valkyrja\Filesystem\InMemoryFilesystem;
26
use Valkyrja\Filesystem\LocalFlysystemFilesystem;
27
use Valkyrja\Filesystem\NullFilesystem;
28
use Valkyrja\Filesystem\S3FlysystemFilesystem;
29
30
/**
31
 * Class ServiceProvider.
32
 *
33
 * @author Melech Mizrachi
34
 */
35
final class ServiceProvider extends Provider
36
{
37
    /**
38
     * @inheritDoc
39
     */
40
    public static function publishers(): array
41
    {
42
        return [
43
            Filesystem::class               => [self::class, 'publishFilesystem'],
44
            FlysystemFilesystem::class      => [self::class, 'publishFlysystemFilesystem'],
45
            LocalFlysystemFilesystem::class => [self::class, 'publishLocalFlysystemFilesystem'],
46
            FlysystemLocalAdapter::class    => [self::class, 'publishFlysystemLocalAdapter'],
47
            S3FlysystemFilesystem::class    => [self::class, 'publishS3FlysystemFilesystem'],
48
            FlysystemAwsS3Adapter::class    => [self::class, 'publishFlysystemAwsS3Adapter'],
49
            InMemoryFilesystem::class       => [self::class, 'publishInMemoryFilesystem'],
50
            NullFilesystem::class           => [self::class, 'publishNullFilesystem'],
51
        ];
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public static function provides(): array
58
    {
59
        return [
60
            Filesystem::class,
61
            FlysystemFilesystem::class,
62
            LocalFlysystemFilesystem::class,
63
            FlysystemLocalAdapter::class,
64
            S3FlysystemFilesystem::class,
65
            FlysystemAwsS3Adapter::class,
66
            InMemoryFilesystem::class,
67
            NullFilesystem::class,
68
        ];
69
    }
70
71
    /**
72
     * Publish the filesystem service.
73
     */
74
    public static function publishFilesystem(Container $container): void
75
    {
76
        $env = $container->getSingleton(Env::class);
77
        /** @var class-string<Filesystem> $default */
78
        $default = $env::FILESYSTEM_DEFAULT;
79
80
        $container->setSingleton(
81
            Filesystem::class,
82
            $container->getSingleton($default),
83
        );
84
    }
85
86
    /**
87
     * Publish the flysystem filesystem service.
88
     */
89
    public static function publishFlysystemFilesystem(Container $container): void
90
    {
91
        $env = $container->getSingleton(Env::class);
92
        /** @var class-string<Filesystem> $default */
93
        $default = $env::FLYSYSTEM_FILESYSTEM_DEFAULT;
94
95
        $container->setSingleton(
96
            FlysystemFilesystem::class,
97
            $container->getSingleton($default),
98
        );
99
    }
100
101
    /**
102
     * Publish the local flysystem filesystem service.
103
     */
104
    public static function publishLocalFlysystemFilesystem(Container $container): void
105
    {
106
        $container->setSingleton(
107
            LocalFlysystemFilesystem::class,
108
            new LocalFlysystemFilesystem(
109
                new Flysystem(
110
                    $container->getSingleton(FlysystemLocalAdapter::class),
111
                )
112
            ),
113
        );
114
    }
115
116
    /**
117
     * Publish the flysystem local adapter service.
118
     */
119
    public static function publishFlysystemLocalAdapter(Container $container): void
120
    {
121
        $env = $container->getSingleton(Env::class);
122
        /** @var non-empty-string $dir */
123
        $dir = $env::FILESYSTEM_FLYSYSTEM_LOCAL_DIR;
124
125
        $container->setSingleton(
126
            FlysystemLocalAdapter::class,
127
            new FlysystemLocalAdapter(
128
                $dir
129
            )
130
        );
131
    }
132
133
    /**
134
     * Publish the s3 flysystem filesystem service.
135
     */
136
    public static function publishS3FlysystemFilesystem(Container $container): void
137
    {
138
        $container->setSingleton(
139
            S3FlysystemFilesystem::class,
140
            new S3FlysystemFilesystem(
141
                new Flysystem(
142
                    $container->getSingleton(FlysystemAwsS3Adapter::class),
143
                )
144
            ),
145
        );
146
    }
147
148
    /**
149
     * Publish the flysystem s3 adapter service.
150
     */
151
    public static function publishFlysystemAwsS3Adapter(Container $container): void
152
    {
153
        $env = $container->getSingleton(Env::class);
154
        /** @var non-empty-string $key */
155
        $key = $env::FILESYSTEM_FLYSYSTEM_S3_KEY;
156
        /** @var non-empty-string $secret */
157
        $secret = $env::FILESYSTEM_FLYSYSTEM_S3_SECRET;
158
        /** @var non-empty-string $region */
159
        $region = $env::FILESYSTEM_FLYSYSTEM_S3_REGION;
160
        /** @var non-empty-string $version */
161
        $version = $env::FILESYSTEM_FLYSYSTEM_S3_VERSION;
162
        /** @var non-empty-string $bucket */
163
        $bucket = $env::FILESYSTEM_FLYSYSTEM_S3_BUCKET;
164
        /** @var string $prefix */
165
        $prefix = $env::FILESYSTEM_FLYSYSTEM_S3_PREFIX;
166
        /** @var array<array-key, mixed> $options */
167
        $options = $env::FILESYSTEM_FLYSYSTEM_S3_OPTIONS;
168
169
        $clientConfig = [
170
            'credentials' => [
171
                'key'    => $key,
172
                'secret' => $secret,
173
            ],
174
            'region'      => $region,
175
            'version'     => $version,
176
        ];
177
178
        $container->setSingleton(
179
            FlysystemAwsS3Adapter::class,
180
            new FlysystemAwsS3Adapter(
181
                client: new AwsS3Client($clientConfig),
182
                bucket: $bucket,
183
                prefix: $prefix,
184
                options: $options
185
            ),
186
        );
187
    }
188
189
    /**
190
     * Publish the in memory filesystem service.
191
     */
192
    public static function publishInMemoryFilesystem(Container $container): void
193
    {
194
        $container->setSingleton(
195
            InMemoryFilesystem::class,
196
            new InMemoryFilesystem(),
197
        );
198
    }
199
200
    /**
201
     * Publish the null filesystem service.
202
     */
203
    public static function publishNullFilesystem(Container $container): void
204
    {
205
        $container->setSingleton(
206
            NullFilesystem::class,
207
            new NullFilesystem(),
208
        );
209
    }
210
}
211