Passed
Pull Request — master (#1)
by Alex
01:36
created

setServiceOptionsKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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