Completed
Push — 2.x ( d6b11c...5f03c1 )
by Akihito
04:42 queued 02:33
created

DependencyProvider::setScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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
     * Provider dependency
13
     *
14
     * @var Dependency
15
     */
16
    private $dependency;
17
18
    /**
19
     * @var bool
20
     */
21
    private $isSingleton = false;
22
23
    /**
24
     * @var mixed
25
     */
26
    private $instance;
27
28 11
    public function __construct(Dependency $dependency)
29
    {
30 11
        $this->dependency = $dependency;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function register(array &$container, Bind $bind)
37
    {
38
        $container[(string) $bind] = $bind->getBound();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function inject(Container $container)
45
    {
46 5
        if ($this->isSingleton && $this->instance) {
47 1
            return $this->instance;
48
        }
49
        $this->instance = $this->dependency->inject($container)->get();
50
51 6
        return $this->instance;
52 5
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function setScope($scope)
58
    {
59 1
        if ($scope === Scope::SINGLETON) {
60 1
            $this->isSingleton = true;
61
        }
62 1
    }
63
64 1
    public function __sleep()
65
    {
66 1
        return ['dependency', 'isSingleton'];
67
    }
68
}
69