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

ServiceInjection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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