Completed
Push — master ( f59311...3e65b5 )
by David
01:42
created

ServiceInjection::getCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
declare (strict_types=1);
3
4
namespace TheCodingMachine\Funky\Injections;
5
6
7
class ServiceInjection implements Injection
8
{
9
    /**
10
     * @var string
11
     */
12
    private $serviceName;
13
    /**
14
     * @var bool
15
     */
16
    private $compulsory;
17
18
    public function __construct(string $serviceName, bool $compulsory)
19
    {
20
        $this->serviceName = $serviceName;
21
        $this->compulsory = $compulsory;
22
    }
23
24
    public function getCode(): string
25
    {
26
        $code = '$container->get('.var_export($this->serviceName, true).')';
27
28
        if (!$this->compulsory) {
29
            $code = sprintf('$container->has(%s)?%s:null', var_export($this->serviceName, true), $code);
30
        }
31
32
        return $code;
33
    }
34
}
35