DirectoryAggregatorFlat   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B aggregates() 0 27 5
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Aggregator;
11
use Hal\Component\Result\ResultCollection;
12
13
/**
14
 * Agregates by directory. Each element is repeated for each namespace
15
 *
16
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
17
 */
18
class DirectoryAggregatorFlat implements Aggregator {
19
20
    /**
21
     * Max depth
22
     *
23
     * @var int
24
     */
25
    private $depth;
26
27
    /**
28
     * Constructor
29
     *
30
     * @param $depth
31
     */
32
    public function __construct($depth)
33
    {
34
        $this->depth = $depth;
35
    }
36
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function aggregates(ResultCollection $results) {
42
        $array = array();
43
        foreach($results as $result) {
44
            $basename = dirname($result->getFilename());
45
46
            // from 'folder1/folder2/file.php', we want an array with ('folder1', 'folder1/folder2')
47
            $namespaces = array_reduce(explode(DIRECTORY_SEPARATOR, $basename), function($v, $e) {
48
                array_push($v, ltrim(end($v).DIRECTORY_SEPARATOR.$e, DIRECTORY_SEPARATOR));
49
                return $v;
50
            }, array());
51
52
            if($this->depth) {
53
                array_splice($namespaces, $this->depth);
54
            }
55
56
            // merge infos for each namespace in the DirectoryResultCollection
57
            foreach($namespaces as $namespace) {
58
59
                if(!isset($array[$namespace])) {
60
                    $array[$namespace] = new ResultCollection();
61
                }
62
63
                $array[$namespace]->push($result);
64
            }
65
        }
66
        return $array;
67
    }
68
}