ServiceStorageProvider::select()   B
last analyzed

Complexity

Conditions 10
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 5
nop 1
crap 10

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 Mathielen\ImportEngine\Storage\Provider;
4
5
use Mathielen\ImportEngine\Storage\ServiceStorage;
6
use Mathielen\ImportEngine\ValueObject\StorageSelection;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
class ServiceStorageProvider implements StorageProviderInterface
10
{
11
    /**
12
     * @var array
13
     *            key = servicename
14
     *            value = array of methods
15
     */
16
    private $services;
17
18
    /**
19
     * @var ContainerInterface
20
     */
21
    private $container;
22
23 6
    public function __construct(ContainerInterface $container, array $services)
24
    {
25 6
        $this->container = $container;
26 6
        $this->services = $services;
27 6
    }
28
29
    /**
30
     * (non-PHPdoc).
31
     *
32
     * @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::storage()
33
     */
34 2
    public function storage(StorageSelection $selection)
35
    {
36 2
        $callable = $selection->getImpl();
37
38 2
        $arguments = array();
39 2
        if (is_array($callable) && isset($callable['arguments'])) {
40 1
            $arguments = $callable['arguments'];
41 1
            unset($callable['arguments']);
42 1
        } elseif (!is_callable($callable)) {
43
            throw new \InvalidArgumentException('StorageSelection must contain a callable or an extended callable (with arguments) as impl');
44
        }
45
46 2
        return new ServiceStorage($callable, $arguments);
47
    }
48
49
    /**
50
     * (non-PHPdoc).
51
     *
52
     * @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::select()
53
     */
54 4
    public function select($id = null)
55
    {
56 4
        if (!is_array($id) || !isset($id['service']) || !isset($id['method']) || empty($id['service']) || empty($id['method'])) {
57 1
            throw new \InvalidArgumentException('Invalid id argument. Must be array and containing at least service and method property.');
58
        }
59
60 3
        $serviceName = $id['service'];
61 3
        if (!isset($this->services[$serviceName])) {
62 1
            throw new \InvalidArgumentException("Service '$serviceName' is not registered in StorageProvider.");
63
        }
64
65 2
        $method = $id['method'];
66 2
        if (!empty($this->services[$serviceName]['methods']) && !in_array($method, $this->services[$serviceName]['methods'])) {
67 1
            throw new \InvalidArgumentException("Method '$method' is not registered in StorageProvider for service '$serviceName'.");
68
        }
69
70 1
        $service = $this->container->get($serviceName);
71 1
        $callable_with_arguments = array($service, $method, isset($id['arguments']) ? $id['arguments'] : null);
72
73 1
        return new StorageSelection($callable_with_arguments);
74
    }
75
}
76