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