Completed
Push — 2.x ( 26569f...004d76 )
by Akihito
02:57
created

DependencyProvider::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
final class DependencyProvider implements DependencyInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    public $context;
15
16
    /**
17
     * Provider dependency
18
     *
19
     * @var Dependency
20
     */
21
    private $dependency;
22
23
    /**
24
     * @var bool
25
     */
26
    private $isSingleton = false;
27
28
    /**
29
     * @var mixed
30
     */
31
    private $instance;
32
33 47
    public function __construct(Dependency $dependency, $context = null)
34
    {
35 47
        $this->dependency = $dependency;
36 47
        $this->context = $context;
37 47
    }
38
39 2
    public function __sleep()
40
    {
41 2
        return ['dependency', 'isSingleton'];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 47
    public function register(array &$container, Bind $bind)
48
    {
49 47
        $container[(string) $bind] = $bind->getBound();
50 47
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 13
    public function inject(Container $container)
56
    {
57 13
        if ($this->isSingleton && $this->instance) {
58 1
            return $this->instance;
59
        }
60 13
        $provider = $this->dependency->inject($container);
61 13
        if ($provider instanceof SetContextInterface) {
62 2
            $this->setContext($provider);
63
        }
64 13
        $this->instance = $provider->get();
65
66 13
        return $this->instance;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 40
    public function setScope($scope)
73
    {
74 40
        if ($scope === Scope::SINGLETON) {
75 40
            $this->isSingleton = true;
76
        }
77 40
    }
78
79 3
    public function setContext(SetContextInterface $provider)
80
    {
81 3
        $provider->setContext($this->context);
82 3
    }
83
}
84