Passed
Push — master ( a657bb...758483 )
by Vitaly
06:27
created

ClosureContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 2
cbo 2
dl 0
loc 68
ccs 26
cts 26
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpaces() 0 5 1
B generateCondition() 0 34 5
A set() 0 11 1
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 26.01.16 at 10:32
5
 */
6
namespace samsonframework\di;
7
8
/**
9
 * Container for resolving closure dependencies.
10
 *
11
 * @package samsonframework\di
12
 */
13
class ClosureContainer extends AbstractContainer
14
{
15 1
    protected function getSpaces($line)
16
    {
17 1
        preg_match('/(\s+)[^\s]/', $line, $matches);
18 1
        return $matches[1];
19
    }
20
    /**
21
     * Generate container dependency condition code.
22
     * @param string    $alias Entity alias
23
     * @param mixed     $entity Entity
24
     */
25 1
    public function generateCondition($alias, &$entity)
26
    {
27
        // Get closure reflection
28 1
        $reflection = new \ReflectionFunction($entity);
29
        // Read closure file
30 1
        $lines = file($reflection->getFileName());
31
32 1
        $indentation = $this->getSpaces($lines[$reflection->getStartLine()]);
33
34 1
        $opened = 0;
35
        // Read only closure lines
36 1
        for ($l = $reflection->getStartLine(); $l < $reflection->getEndLine(); $l++) {
37
            // Fix opening braces scope
38 1
            if (strpos($lines[$l], '{') !== false) {
39 1
                $opened++;
40 1
            }
41
42
            // Fix closing braces scope
43 1
            if (strpos($lines[$l], '}') !== false) {
44 1
                $opened--;
45 1
            }
46
47
            // Break if we reached closure end
48 1
            if ($opened === -1) {
49 1
                break;
50
            }
51
52
            // Cut only base $indentation to beautify output
53 1
            $spaces = substr($this->getSpaces($lines[$l]), strlen($indentation));
54
55
            // Add closure code
56 1
            $this->generator->newLine($spaces.trim($lines[$l]));
57 1
        }
58 1
    }
59
60
    /**
61
     * Set container dependency.
62
     *
63
     * @param callable    $entity     Callable
64
     * @param string|null $alias      Entity alias for simplier finding
65
     * @param array       $parameters Collection of additional parameters
66
     *
67
     * @return self Chaining
68
     */
69 6
    public function set($entity, $alias = null, array $parameters = array())
70
    {
71
        // Add unique closure alias
72 6
        $this->aliases[$alias] = 'closure' . uniqid(0, 99999);
73
74
        // Store parameters
75 6
        $this->parameters[$alias] = $parameters;
76
77
        // Store dependency
78 6
        $this->dependencies[$alias] = $entity;
79 6
    }
80
}
81