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

RackspaceAdapterFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 3.85 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 15
c 10
b 0
f 0
lcom 1
cbo 7
dl 3
loc 78
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateConfig() 3 32 12
B doCreateService() 0 36 3

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
namespace BsbFlysystem\Adapter\Factory;
4
5
use BsbFlysystem\Exception\RequirementsException;
6
use BsbFlysystem\Exception\UnexpectedValueException;
7
use League\Flysystem\Rackspace\RackspaceAdapter as Adapter;
8
use OpenCloud\OpenStack;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
11
class RackspaceAdapterFactory extends AbstractAdapterFactory
12
{
13
    /**
14
     * @inheritdoc
15
     */
16 1
    public function doCreateService(ServiceLocatorInterface $serviceLocator)
17
    {
18
        if (!class_exists(\League\Flysystem\Rackspace\RackspaceAdapter::class) ||
19
            !class_exists(\ProxyManager\Factory\LazyLoadingValueHolderFactory::class)
20
        ) {
21
            throw new RequirementsException(
22
                ['league/flysystem-rackspace', 'ocramius/proxy-manager'],
23
                'Rackspace'
24
            );
25
        }
26
27 1
        $proxy = $this->getLazyFactory($serviceLocator)->createProxy(
28
            \League\Flysystem\Rackspace\RackspaceAdapter::class,
29 1
            function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) {
30
                $client = new OpenStack(
31
                    $this->options['url'],
32
                    $this->options['secret'],
33
                    $this->options['options']
34
                );
35
36
                $store = $client->objectStoreService(
37
                    $this->options['objectstore']['name'],
38
                    $this->options['objectstore']['region'],
39
                    $this->options['objectstore']['url_type']
40
                );
41
42
                $container = $store->getContainer($this->options['objectstore']['container']);
43
44
                $wrappedObject = new Adapter($container, $this->options['prefix']);
45
46
                return true;
47 1
            }
48
        );
49
50 1
        return $proxy;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    protected function validateConfig()
57
    {
58
        if (!isset($this->options['url'])) {
59
            throw new UnexpectedValueException("Missing 'url' as option");
60
        }
61
62 View Code Duplication
        if (!isset($this->options['secret']) || !is_array($this->options['secret'])) {
63
            throw new UnexpectedValueException("Missing 'secret' as option");
64
        }
65
66
        if (!isset($this->options['objectstore']) || !is_array($this->options['objectstore'])) {
67
            throw new UnexpectedValueException("Missing 'objectstore' as option");
68
        } elseif (!isset($this->options['objectstore']['name'])) {
69
            throw new UnexpectedValueException("Missing 'objectstore.name' as option");
70
        } elseif (!isset($this->options['objectstore']['region'])) {
71
            throw new UnexpectedValueException("Missing 'objectstore.region' as option");
72
        } elseif (!isset($this->options['objectstore']['container'])) {
73
            throw new UnexpectedValueException("Missing 'objectstore.container' as option");
74
        }
75
76
        if (!isset($this->options['objectstore']['url_type'])) {
77
            $this->options['objectstore']['url_type'] = null;
78
        }
79
80
        if (!isset($this->options['options'])) {
81
            $this->options['options'] = [];
82
        }
83
84
        if (!isset($this->options['prefix'])) {
85
            $this->options['prefix'] = null;
86
        }
87
    }
88
}
89