AwsS3v3AdapterFactory::validateConfig()   C
last analyzed

Complexity

Conditions 13
Paths 23

Size

Total Lines 36

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 12
CRAP Score 13

Importance

Changes 0
Metric Value
dl 3
loc 36
ccs 12
cts 12
cp 1
rs 6.6166
c 0
b 0
f 0
cc 13
nc 23
nop 0
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * BsbFlystem
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @see       https://bushbaby.nl/
10
 *
11
 * @copyright Copyright (c) 2014-2021 bushbaby multimedia. (https://bushbaby.nl)
12
 * @author    Bas Kamer <[email protected]>
13
 * @license   MIT
14
 *
15
 * @package   bushbaby/flysystem
16
 */
17
18
declare(strict_types=1);
19
20
namespace BsbFlysystem\Adapter\Factory;
21
22
use Aws\S3\S3Client;
23
use BsbFlysystem\Exception\RequirementsException;
24
use BsbFlysystem\Exception\UnexpectedValueException;
25
use League\Flysystem\AdapterInterface;
26
use League\Flysystem\AwsS3v3\AwsS3Adapter as Adapter;
27
use Psr\Container\ContainerInterface;
28
29
class AwsS3v3AdapterFactory extends AbstractAdapterFactory
30
{
31
    public function doCreateService(ContainerInterface $container): AdapterInterface
32
    {
33
        if (! \class_exists(Adapter::class)) {
34
            throw new RequirementsException(['league/flysystem-aws-s3-v3'], 'AwsS3v3');
35
        }
36
        $config = [
37
            'region' => $this->options['region'],
38
            'version' => $this->options['version'],
39
            'http' => $this->options['request.options'],
40
        ];
41
42
        // Override the endpoint for example: when using an s3 compatible host
43
        if (! empty($this->options['endpoint'])) {
44
            $config['endpoint'] = $this->options['endpoint'];
45
        }
46
47
        if (! empty($this->options['use_path_style_endpoint'])) {
48
            $config['use_path_style_endpoint'] = $this->options['use_path_style_endpoint'];
49
        }
50
51
        if (! isset($this->options['iam']) || (isset($this->options['iam']) && (false === $this->options['iam']))) {
52
            $credentials = [
53
                'key' => $this->options['credentials']['key'],
54 9
                'secret' => $this->options['credentials']['secret'],
55
            ];
56 9
            $config = \array_merge(\compact('credentials'), $config);
57 7
        }
58 2
59
        $adapterOptions = $this->options['options'] ?? [];
60
61 5
        $client = new S3Client($config);
62 1
63
        return new Adapter($client, $this->options['bucket'], $this->options['prefix'], $adapterOptions);
64
    }
65 4
66 1
    protected function validateConfig(): void
67
    {
68
        if (! isset($this->options['iam']) || (isset($this->options['iam']) && (false === $this->options['iam']))) {
69 View Code Duplication
            if (! isset($this->options['credentials']) || ! \is_array($this->options['credentials'])) {
70 5
                throw new UnexpectedValueException("Missing 'credentials' as array");
71 3
            }
72
73
            if (! isset($this->options['credentials']['key'])) {
74 2
                throw new UnexpectedValueException("Missing 'key' as option");
75 1
            }
76
77
            if (! isset($this->options['credentials']['secret'])) {
78 1
                throw new UnexpectedValueException("Missing 'secret' as option");
79 1
            }
80
        }
81
82 1
        if (! isset($this->options['region'])) {
83 1
            throw new UnexpectedValueException("Missing 'region' as option");
84
        }
85
86 1
        if (! isset($this->options['bucket'])) {
87 1
            throw new UnexpectedValueException("Missing 'bucket' as option");
88
        }
89 1
90
        if (! isset($this->options['version'])) {
91
            $this->options['version'] = 'latest';
92
        }
93
94
        if (! isset($this->options['prefix'])) {
95
            $this->options['prefix'] = '';
96
        }
97
98
        if (! isset($this->options['request.options'])) {
99
            $this->options['request.options'] = [];
100
        }
101
    }
102
}
103