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

setApplicationOptionsKey()   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 Arp\LaminasFactory\Exception\ServiceNotFoundException;
9
use Psr\Container\ContainerInterface;
10
11
/**
12
 * Trait used to provided the application options to a factory.
13
 *
14
 * @author  Alex Patterson <[email protected]>
15
 * @package Arp\LaminasFactory
16
 */
17
trait ApplicationOptionsProviderTrait
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $applicationOptionsKey = 'arp';
23
24
    /**
25
     * @var string
26
     */
27
    private $applicationOptionsService = 'config';
28
29
    /**
30
     * Return an array of application options.
31
     *
32
     * @param ContainerInterface $container
33
     * @param string|null        $optionsKey
34
     *
35
     * @return array
36
     *
37
     * @throws ServiceNotFoundException
38
     */
39
    public function getApplicationOptions(ContainerInterface $container, string $optionsKey = null): array
40
    {
41
        if (null !== $optionsKey) {
42
            $this->setApplicationOptionsKey($optionsKey);
43
        }
44
45
        if (!$container->has($this->applicationOptionsService)) {
46
            throw new ServiceNotFoundException(
47
                sprintf(
48
                    'The required application options service \'%s\' could not be found in \'%s\'.',
49
                    $this->applicationOptionsService,
50
                    __METHOD__
51
                )
52
            );
53
        }
54
55
        $options = $container->get($this->applicationOptionsService);
56
57
        if (!is_array($options) || !array_key_exists($this->applicationOptionsKey, $options)) {
58
            throw new ServiceNotCreatedException(
59
                sprintf(
60
                    'The application key \'%s\' could not be found within the application options service \'%s\'.',
61
                    $this->applicationOptionsKey,
62
                    $this->applicationOptionsService
63
                )
64
            );
65
        }
66
67
        $options = $options[$this->applicationOptionsKey];
68
69
        if ($options instanceof \Traversable) {
70
            $options = iterator_to_array($options, true);
71
        }
72
73
        if (!is_array($options)) {
74
            throw new ServiceNotCreatedException(
75
                sprintf(
76
                    'The application options must be an \'array\' or object of type \'%s\'; \'%s\' provided in \'%s\'.',
77
                    \Traversable::class,
78
                    gettype($options),
79
                    __METHOD__
80
                )
81
            );
82
        }
83
84
        return $options;
85
    }
86
87
    /**
88
     * @param string $optionsKey
89
     */
90
    public function setApplicationOptionsKey(string $optionsKey): void
91
    {
92
        $this->applicationOptionsKey = $optionsKey;
93
    }
94
}
95