Completed
Push — master ( 455325...7625db )
by Bas
06:55 queued 02:45
created

RackspaceAdapterFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 4.11 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 7
dl 3
loc 73
ccs 28
cts 31
cp 0.9032
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validateConfig() 3 32 12
B doCreateService() 0 37 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
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