Dependency::object()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 15
rs 10
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * Dependency target
4
 * User: moyo
5
 * Date: 12/10/2017
6
 * Time: 11:16 AM
7
 */
8
9
namespace Carno\Container\Injection;
10
11
use Carno\Container\Container;
12
use Carno\Container\Exception\DependsInterfaceNotAssignedException;
13
14
class Dependency
15
{
16
    /**
17
     * @var Container
18
     */
19
    private $di = null;
20
21
    /**
22
     * @var string
23
     */
24
    private $class = null;
25
26
    /**
27
     * @var bool
28
     */
29
    private $contract = false;
30
31
    /**
32
     * @var bool
33
     */
34
    private $optional = false;
35
36
    /**
37
     * Dependency constructor.
38
     * @param Container $di
39
     * @param string $class
40
     * @param bool $contract
41
     * @param bool $optional
42
     */
43
    public function __construct(Container $di, string $class, bool $contract = false, bool $optional = false)
44
    {
45
        $this->di = $di;
46
        $this->class = $class;
47
        $this->contract = $contract;
48
        $this->optional = $optional;
49
    }
50
51
    /**
52
     * @return object|mixed|null
53
     */
54
    public function object()
55
    {
56
        if ($this->di->has($this->class)) {
57
            return $this->di->get($this->class);
58
        }
59
60
        if ($this->contract) {
61
            if ($this->optional) {
62
                return null;
63
            }
64
65
            throw new DependsInterfaceNotAssignedException($this->class);
66
        }
67
68
        return $this->di->set($this->class, $this->di->object($this->class));
69
    }
70
}
71