Completed
Pull Request — 1.2 (#12)
by David
01:36
created

cloneWithoutIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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(): ?string
52
    {
53
        return $this->identifier;
54
    }
55
56
    /**
57
     * Returns an InlineEntryInterface object representing the PHP code necessary to generate
58
     * the container entry.
59
     *
60
     * @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container"
61
     * @param array  $usedVariables     An array of variables that are already used and that should not be used when generating this code.
62
     *
63
     * @return InlineEntryInterface
64
     */
65
    public function toPhpCode(string $containerVariable, array $usedVariables = array()): InlineEntryInterface
66
    {
67
        $code = sprintf('$this->registry->createService(%s, %s, $this->delegateLookupContainer)', var_export($this->serviceProviderKey, true),
68
            var_export($this->serviceName, true));
69
70
        return new InlineEntry($code, null, $usedVariables);
71
    }
72
73
    public function cloneWithoutIdentifier(): self
74
    {
75
        return new self(null, $this->serviceName, $this->serviceProviderKey);
76
    }
77
}
78