AwsS3v3AdapterFactory   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 74
Duplicated Lines 4.05 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 63.33%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 3
loc 74
ccs 19
cts 30
cp 0.6333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B doCreateService() 0 34 7
C validateConfig() 3 36 13

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
/**
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