Completed
Push — master ( 153a9a...119967 )
by Alex
14s queued 12s
created

getServiceOptionsKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasFactory;
6
7
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
8
use Laminas\ServiceManager\AbstractPluginManager;
9
use Psr\Container\ContainerInterface;
10
11
/**
12
 * Trait used to provide service options to factories.
13
 *
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\LaminasFactory
16
 */
17
trait ServiceOptionsProviderTrait
18
{
19
    /**
20
     * @trait ApplicationOptionsProviderTrait
21
     */
22
    use ApplicationOptionsProviderTrait;
23
24
    /**
25
     * @var string
26
     */
27
    private $serviceOptionsKey = 'services';
28
29
    /**
30
     * @param ContainerInterface $container     The dependency injection container.
31
     * @param string             $requestedName The name of the service being created.
32
     * @param string             $key           The type of service that should be checked.
33
     *
34
     * @return array
35
     *
36
     * @throws ServiceNotCreatedException  If the service options cannot be loaded or are invalid.
37
     */
38
    public function getServiceOptions(ContainerInterface $container, string $requestedName, $key = null): array
39
    {
40
        $applicationOptions = $this->getApplicationOptions($container);
41
        $serviceOptionsKey = $this->getServiceOptionsKey($key);
42
43
        if (!array_key_exists($serviceOptionsKey, $applicationOptions)) {
44
            throw new ServiceNotCreatedException(
45
                sprintf(
46
                    'Unable to find a configuration key for service of type \'%s\' while creating service \'%s\'.',
47
                    $serviceOptionsKey,
48
                    $requestedName
49
                )
50
            );
51
        }
52
53
        $serviceOptions = (isset($this->factoryOptions) && is_array($this->factoryOptions))
54
            ? $this->factoryOptions
55
            : [];
56
57
        if (isset($applicationOptions[$serviceOptionsKey][$requestedName])) {
58
            if (!is_array($applicationOptions[$serviceOptionsKey][$requestedName])) {
59
                throw new ServiceNotCreatedException(
60
                    sprintf(
61
                        'The configuration options must be of type \'array\'; \'%s\' provided for service \'%s\'.',
62
                        gettype($applicationOptions[$serviceOptionsKey][$requestedName]),
63
                        $requestedName
64
                    )
65
                );
66
            }
67
            $serviceOptions = array_replace_recursive(
68
                $serviceOptions,
69
                $applicationOptions[$serviceOptionsKey][$requestedName]
70
            );
71
        }
72
73
        return $serviceOptions;
74
    }
75
76
    /**
77
     * @param string|null $key
78
     *
79
     * @return string
80
     */
81
    private function getServiceOptionsKey(?string $key): string
82
    {
83
        if (null === $key) {
84
            return $this->serviceOptionsKey;
85
        }
86
87
        return $key;
88
    }
89
}
90