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

DependencyProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 72.21%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 8
c 6
b 1
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 13
cts 18
cp 0.7221
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __sleep() 0 4 1
A register() 0 4 1
A __construct() 0 4 1
A inject() 0 9 3
A setScope() 0 6 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