Completed
Push — master ( 455325...7625db )
by Bas
06:55 queued 02:45
created

AwsS3v3AdapterFactory::validateConfig()   C

Complexity

Conditions 13
Paths 23

Size

Total Lines 36
Code Lines 18

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 19
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 3
loc 36
ccs 19
cts 19
cp 1
rs 5.1234
c 1
b 0
f 0
cc 13
eloc 18
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
declare(strict_types=1);
4
5
namespace BsbFlysystem\Adapter\Factory;
6
7
use Aws\S3\S3Client;
8
use BsbFlysystem\Exception\RequirementsException;
9
use BsbFlysystem\Exception\UnexpectedValueException;
10
use League\Flysystem\AdapterInterface;
11
use League\Flysystem\AwsS3v3\AwsS3Adapter as Adapter;
12
use Zend\ServiceManager\ServiceLocatorInterface;
13
14
class AwsS3v3AdapterFactory extends AbstractAdapterFactory
15
{
16
    public function doCreateService(ServiceLocatorInterface $serviceLocator): AdapterInterface
17
    {
18
        if (! class_exists(\League\Flysystem\AwsS3v3\AwsS3Adapter::class)) {
19
            throw new RequirementsException(
20
                ['league/flysystem-aws-s3-v3'],
21
                'AwsS3v3'
22
            );
23
        }
24
        $config = [
25
            'region'          => $this->options['region'],
26
            'version'         => $this->options['version'],
27
            'request.options' => $this->options['request.options'],
28
        ];
29
30
        if (! isset($this->options['iam']) || (isset($this->options['iam']) && (false === $this->options['iam']))) {
31
            $credentials = [
32
                'key'    => $this->options['credentials']['key'],
33
                'secret' => $this->options['credentials']['secret'],
34
            ];
35
            $config = array_merge(compact('credentials'), $config);
36
        }
37
38
        $client = new S3Client($config);
39
40
        $adapter = new Adapter($client, $this->options['bucket'], $this->options['prefix']);
41
42
        return $adapter;
43
    }
44
45 9
    protected function validateConfig()
46
    {
47 9
        if (! isset($this->options['iam']) || (isset($this->options['iam']) && (false === $this->options['iam']))) {
48 7 View Code Duplication
            if (! isset($this->options['credentials']) || ! is_array($this->options['credentials'])) {
49 2
                throw new UnexpectedValueException("Missing 'credentials' as array");
50
            }
51
52 5
            if (! isset($this->options['credentials']['key'])) {
53 1
                throw new UnexpectedValueException("Missing 'key' as option");
54
            }
55
56 4
            if (! isset($this->options['credentials']['secret'])) {
57 1
                throw new UnexpectedValueException("Missing 'secret' as option");
58
            }
59
        }
60
61 5
        if (! isset($this->options['region'])) {
62 3
            throw new UnexpectedValueException("Missing 'region' as option");
63
        }
64
65 2
        if (! isset($this->options['bucket'])) {
66 1
            throw new UnexpectedValueException("Missing 'bucket' as option");
67
        }
68
69 1
        if (! isset($this->options['version'])) {
70 1
            $this->options['version'] = 'latest';
71
        }
72
73 1
        if (! isset($this->options['prefix'])) {
74 1
            $this->options['prefix'] = '';
75
        }
76
77 1
        if (! isset($this->options['request.options'])) {
78 1
            $this->options['request.options'] = [];
79
        }
80 1
    }
81
}
82