Shared   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 90
rs 10
ccs 16
cts 16
cp 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getService() 0 16 3
A __construct() 0 13 1
1
<?php
2
3
/**
4
 * Shared.php
5
 *
6
 * @author Dominik Kocuj
7
 * @license https://opensource.org/licenses/MIT The MIT License
8
 * @copyright Copyright (c) 2017-2018 kocuj.pl
9
 */
10
11
namespace Kocuj\Di\Service\Shared;
12
13
use Kocuj\Di\ArgumentParser\ArgumentParserFactoryInterface;
14
use Kocuj\Di\Container\ContainerInterface;
15
use Kocuj\Di\Service\ServiceInterface;
16
17
/**
18
 * Shared service creator
19
 *
20
 * @package Kocuj\Di\Service\Shared
21
 */
22
class Shared implements ServiceInterface
23
{
24
    /**
25
     * Service argument parser factory
26
     *
27
     * @var ArgumentParserFactoryInterface
28
     */
29
    private $argumentParserFactory;
30
31
    /**
32
     * Dependency injection container for services
33
     *
34
     * @var ContainerInterface
35
     */
36
    private $container;
37
38
    /**
39
     * Service identifier
40
     *
41
     * @var string
42
     */
43
    private $id;
44
45
    /**
46
     * Source for service to create
47
     *
48
     * @var string
49
     */
50
    private $source;
51
52
    /**
53
     * Service arguments to parse
54
     *
55
     * @var array
56
     */
57
    private $arguments;
58
59
    /**
60
     * Service object
61
     *
62
     * @var object
63
     */
64
    private $serviceObject = null;
65
66
    /**
67
     * Constructor
68
     *
69
     * @param ArgumentParserFactoryInterface $argumentParserFactory Service argument parser factory
70
     * @param ContainerInterface $container Dependency injection container for services
71
     * @param string $id Service identifier
72
     * @param string $source Source for service to create
73
     * @param array $arguments Service arguments to parse
74
     */
75 12
    public function __construct(
76
        ArgumentParserFactoryInterface $argumentParserFactory,
77
        ContainerInterface $container,
78
        string $id,
79
        string $source,
80
        array $arguments = []
81
    ) {
82
        // remember arguments
83 12
        $this->argumentParserFactory = $argumentParserFactory;
84 12
        $this->container = $container;
85 12
        $this->id = $id;
86 12
        $this->source = $source;
87 12
        $this->arguments = $arguments;
88 12
    }
89
90
    /**
91
     * Get service
92
     *
93
     * @return object Service object
94
     * @see \Kocuj\Di\Service\ServiceInterface::getService()
95
     */
96 12
    public function getService()
97
    {
98
        // optionally use shared object
99 12
        if (!is_null($this->serviceObject)) {
100 12
            return $this->serviceObject;
101
        }
102
        // parse arguments
103 12
        $parsedArgs = [];
104 12
        foreach ($this->arguments as $argument) {
105 6
            $obj = $this->argumentParserFactory->create($this->container, $this->id, $argument);
106 6
            $parsedArgs[] = $obj->parse();
107
        }
108
        // execute service constructor
109 12
        $this->serviceObject = new $this->source(...$parsedArgs);
110
        // exit
111 12
        return $this->serviceObject;
112
    }
113
}
114