AddToArray::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Interop\Container\Factories;
4
5
use Interop\Container\ContainerInterface;
6
7
/**
8
 * A factory that extends an existing array by adding an additional service to it.
9
 */
10
final class AddToArray
11
{
12
    private $serviceName;
13
14
    /**
15
     * @param string $serviceName The identifier of the service that is added to the array
16
     */
17
    public function __construct($serviceName)
18
    {
19
        $this->serviceName = $serviceName;
20
    }
21
22
    /**
23
     * Returns the identifier of the service that is added to the array.
24
     *
25
     * @return string
26
     */
27
    public function getAddedService()
28
    {
29
        return $this->serviceName;
30
    }
31
32
    /**
33
     * Extend the array by adding a new service to it.
34
     * The array is created if it does not exist yet.
35
     *
36
     * @param ContainerInterface $container
37
     * @param array              $previous
38
     *
39
     * @return array
40
     */
41
    public function __invoke(ContainerInterface $container, array $previous = [])
42
    {
43
        $previous[] = $container->get($this->serviceName);
44
        return $previous;
45
    }
46
}
47