Completed
Push — master ( e81256...4b285b )
by Bas
04:59
created

AwsS3AdapterFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 9
c 9
b 0
f 0
lcom 1
cbo 3
dl 0
loc 57
ccs 0
cts 11
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateConfig() 0 26 7
A doCreateService() 0 20 2
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\AwsS3v2\AwsS3Adapter as Adapter;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
class AwsS3AdapterFactory extends AbstractAdapterFactory
12
{
13
14
    /**
15
     * @inheritdoc
16
     */
17
    public function doCreateService(ServiceLocatorInterface $serviceLocator)
18
    {
19
        if (!class_exists(\League\Flysystem\AwsS3v2\AwsS3Adapter::class)) {
20
            throw new RequirementsException(
21
                ['league/flysystem-aws-s3-v2'],
22
                'AwsS3'
23
            );
24
        }
25
26
        $client = S3Client::factory([
27
            'key'    => $this->options['key'],
28
            'secret' => $this->options['secret'],
29
            'region' => $this->options['region'],
30
            'request.options' => $this->options['request.options'],
31
        ]);
32
33
        $adapter = new Adapter($client, $this->options['bucket'], $this->options['prefix']);
34
35
        return $adapter;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    protected function validateConfig()
42
    {
43
        if (!isset($this->options['key'])) {
44
            throw new UnexpectedValueException("Missing 'key' as option");
45
        }
46
47
        if (!isset($this->options['secret'])) {
48
            throw new UnexpectedValueException("Missing 'secret' as option");
49
        }
50
51
        if (!isset($this->options['region'])) {
52
            throw new UnexpectedValueException("Missing 'region' as option");
53
        }
54
55
        if (!isset($this->options['bucket'])) {
56
            throw new UnexpectedValueException("Missing 'bucket' as option");
57
        }
58
59
        if (!isset($this->options['prefix'])) {
60
            $this->options['prefix'] = null;
61
        }
62
63
        if (!isset($this->options['request.options'])) {
64
            $this->options['request.options'] = [];
65
        }
66
    }
67
}
68