Completed
Pull Request — 1.1 (#10)
by David
07:43
created

CreateServiceFromRegistryDefinition   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 77
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIdentifier() 0 4 1
A getServiceProviderKey() 0 4 1
A getServiceName() 0 4 1
A toPhpCode() 0 7 1
1
<?php
2
3
namespace TheCodingMachine\Yaco\ServiceProvider;
4
5
use TheCodingMachine\Yaco\Definition\DumpableInterface;
6
use TheCodingMachine\Yaco\Definition\InlineEntry;
7
use TheCodingMachine\Yaco\Definition\InlineEntryInterface;
8
9
/**
10
 * Fetches a service from the service-providers registry.
11
 */
12
class CreateServiceFromRegistryDefinition implements DumpableInterface
13
{
14
    /**
15
     * The identifier of the instance in the container.
16
     *
17
     * @var string|null
18
     */
19
    private $identifier;
20
21
    /**
22
     * The key of the service provider in the registry.
23
     *
24
     * @var int
25
     */
26
    private $serviceProviderKey;
27
28
    /**
29
     * @var string
30
     */
31
    private $serviceName;
32
33
    /**
34
     * @param string|null                    $identifier
35
     * @param string                         $serviceName
36
     * @param int                            $serviceProviderKey
37
     */
38
    public function __construct($identifier, string $serviceName, int $serviceProviderKey)
39
    {
40
        $this->identifier = $identifier;
41
        $this->serviceName = $serviceName;
42
        $this->serviceProviderKey = $serviceProviderKey;
43
    }
44
45
    /**
46
     * Returns the identifier for this object in the container.
47
     * If null, classes consuming this definition should assume the definition must be inlined.
48
     *
49
     * @return string|null
50
     */
51
    public function getIdentifier()
52
    {
53
        return $this->identifier;
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    public function getServiceProviderKey(): int
60
    {
61
        return $this->serviceProviderKey;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getServiceName(): string
68
    {
69
        return $this->serviceName;
70
    }
71
72
    /**
73
     * Returns an InlineEntryInterface object representing the PHP code necessary to generate
74
     * the container entry.
75
     *
76
     * @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container"
77
     * @param array  $usedVariables     An array of variables that are already used and that should not be used when generating this code.
78
     *
79
     * @return InlineEntryInterface
80
     */
81
    public function toPhpCode($containerVariable, array $usedVariables = array())
82
    {
83
        $code = sprintf('$this->registry->createService(%s, %s, $this->delegateLookupContainer)', var_export($this->serviceProviderKey, true),
84
            var_export($this->serviceName, true));
85
86
        return new InlineEntry($code, null, $usedVariables);
87
    }
88
}
89