ServiceInjection::getCode()   A
last analyzed

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