Completed
Push — master ( a0e8ce...60fa13 )
by Bas
12s
created

RackspaceAdapterFactory::validateConfig()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 32
Code Lines 19

Duplication

Lines 3
Ratio 9.38 %

Code Coverage

Tests 20
CRAP Score 12

Importance

Changes 0
Metric Value
dl 3
loc 32
ccs 20
cts 20
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 19
nc 14
nop 0
crap 12

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 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 1
        if (!class_exists(\League\Flysystem\Rackspace\RackspaceAdapter::class) ||
19 1
            !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 1
            \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 10
    protected function validateConfig()
57
    {
58 10
        if (!isset($this->options['url'])) {
59 1
            throw new UnexpectedValueException("Missing 'url' as option");
60
        }
61
62 9 View Code Duplication
        if (!isset($this->options['secret']) || !is_array($this->options['secret'])) {
63 2
            throw new UnexpectedValueException("Missing 'secret' as option");
64
        }
65
66 7
        if (!isset($this->options['objectstore']) || !is_array($this->options['objectstore'])) {
67 2
            throw new UnexpectedValueException("Missing 'objectstore' as option");
68 5
        } elseif (!isset($this->options['objectstore']['name'])) {
69 1
            throw new UnexpectedValueException("Missing 'objectstore.name' as option");
70 4
        } elseif (!isset($this->options['objectstore']['region'])) {
71 1
            throw new UnexpectedValueException("Missing 'objectstore.region' as option");
72 3
        } elseif (!isset($this->options['objectstore']['container'])) {
73 1
            throw new UnexpectedValueException("Missing 'objectstore.container' as option");
74
        }
75
76 2
        if (!isset($this->options['objectstore']['url_type'])) {
77 1
            $this->options['objectstore']['url_type'] = null;
78
        }
79
80 2
        if (!isset($this->options['options'])) {
81 2
            $this->options['options'] = [];
82
        }
83
84 2
        if (!isset($this->options['prefix'])) {
85 2
            $this->options['prefix'] = null;
86
        }
87 2
    }
88
}
89