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

AwsS3v3AdapterFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 68
Duplicated Lines 4.41 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 1
cbo 5
dl 3
loc 68
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateConfig() 3 36 13
B doCreateService() 0 28 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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