Completed
Push — master ( ccb9d5...d7e68d )
by Alejandro
09:04
created

readKeysFromArray()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 3
nop 2
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Factory;
5
6
use Interop\Container\ContainerInterface;
7
use Interop\Container\Exception\ContainerException;
8
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
12
13
class DottedAccessConfigAbstractFactory implements AbstractFactoryInterface
14
{
15
    /**
16
     * Can the factory create an instance for the service?
17
     *
18
     * @param  ContainerInterface $container
19
     * @param  string $requestedName
20
     * @return bool
21
     */
22 4
    public function canCreate(ContainerInterface $container, $requestedName)
23
    {
24 4
        return substr_count($requestedName, '.') > 0;
25
    }
26
27
    /**
28
     * Create an object
29
     *
30
     * @param  ContainerInterface $container
31
     * @param  string $requestedName
32
     * @param  null|array $options
33
     * @return object
34
     * @throws InvalidArgumentException
35
     * @throws ServiceNotFoundException if unable to resolve the service.
36
     * @throws ServiceNotCreatedException if an exception is raised when
37
     *     creating a service.
38
     * @throws ContainerException if any other error occurs
39
     */
40 3
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
41
    {
42 3
        $parts = explode('.', $requestedName);
43 3
        $serviceName = array_shift($parts);
44 3
        if (! $container->has($serviceName)) {
45 1
            throw new ServiceNotCreatedException(sprintf(
46 1
                'Defined service "%s" could not be found in container after resolving dotted expression "%s".',
47 1
                $serviceName,
48 1
                $requestedName
49
            ));
50
        }
51
52 2
        $array = $container->get($serviceName);
53 2
        return $this->readKeysFromArray($parts, $array);
54
    }
55
56
    /**
57
     * @param array $keys
58
     * @param array|\ArrayAccess $array
59
     * @return mixed|null
60
     * @throws  InvalidArgumentException
61
     */
62 2
    private function readKeysFromArray(array $keys, $array)
63
    {
64 2
        $key = array_shift($keys);
65
66
        // When one of the provided keys is not found, throw an exception
67 2
        if (! isset($array[$key])) {
68 1
            throw new InvalidArgumentException(sprintf(
69 1
                'The key "%s" provided in the dotted notation could not be found in the array service',
70 1
                $key
71
            ));
72
        }
73
74 2
        $value = $array[$key];
75 2
        if (! empty($keys) && (is_array($value) || $value instanceof \ArrayAccess)) {
76 2
            $value = $this->readKeysFromArray($keys, $value);
77
        }
78
79 1
        return $value;
80
    }
81
}
82