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