Html::prepareDataRelations()   C
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 5.3846
cc 8
eloc 17
nc 12
nop 1
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\Application\Formater\Summary;
11
use Hal\Application\Config\TemplateConfiguration;
12
use Hal\Application\Formater\FormaterInterface;
13
use Hal\Application\Formater\Twig\FormatingExtension;
14
use Hal\Application\Rule\Validator;
15
use Hal\Component\Bounds\BoundsInterface;
16
use Hal\Component\Result\ResultCollection;
17
18
19
/**
20
 * Formater for html export
21
 *
22
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
23
 */
24
class Html implements FormaterInterface {
25
26
    /**
27
     * Bounds
28
     *
29
     * @var BoundsInterface
30
     */
31
    private $bound;
32
33
    /**
34
     * Validator
35
     *
36
     * @var Validator
37
     */
38
    private $validator;
39
40
    /**
41
     * Template configuration
42
     *
43
     * @var TemplateConfiguration
44
     */
45
    private $template;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param Validator             $validator
51
     * @param BoundsInterface       $bound
52
     * @param TemplateConfiguration $template
53
     */
54
    public function __construct(Validator $validator, BoundsInterface $bound, TemplateConfiguration $template)
55
    {
56
        $this->bound = $bound;
57
        $this->validator = $validator;
58
        $this->template = $template;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function terminate(ResultCollection $collection, ResultCollection $groupedResults){
65
        \Twig_Autoloader::register();
66
        $loader = new \Twig_Loader_Filesystem(__DIR__.'/../../../../../templates/html');
67
        $twig = new \Twig_Environment($loader, array('cache' => false));
68
        $twig->addExtension(new FormatingExtension($this->validator));
69
70
        $bound = $this->bound->calculate($collection);
71
        return $twig->render('summary/report.html.twig', array(
72
            'keys' => array_keys(current($collection->asArray()))
73
            , 'results' => $collection->asArray()
74
            , 'groupedResults' => $groupedResults
75
            , 'root' => $groupedResults->getIterator()->current()
76
            , 'relations' => $this->prepareDataRelations($collection)
77
            , 'scores' => $collection->getScore()->all()
78
            , 'ruleSet' => $this->validator->getRuleSet()
79
            , 'bounds' => $bound
80
            , 'withOOP' => null !== $bound->getSum('instability')
81
            , 'title' => $this->template->getTitle()
82
        ));
83
    }
84
85
    /**
86
     * Build flat array of relations
87
     *
88
     * @param ResultCollection $collection
89
     * @return array
90
     */
91
    private function prepareDataRelations(ResultCollection $collection) {
92
        $array = array();
93
94
        // map of classes an relations
95
        foreach($collection as $item) {
96
97
            // case of oop is disabled
98
            if(!$item->getOOP()) {
99
                continue;
100
            }
101
102
            foreach($item->getOOP()->getClasses() as $class) {
103
                $array[$class->getFullname()] = (object) array(
104
                    'name' => $class->getFullname()
105
                    , 'size' => 3000
106
                    , 'relations' => array_merge(
107
                        !is_null($class->getParent()) ? array($class->getParent()) : array()
108
                        , $class->getDependencies()
109
                    )
110
                );
111
            }
112
        }
113
114
        // dependency can not be in the parsed sources (for example, native PHP classes)
115
        foreach($array as $class => $infos) {
116
            foreach($infos->relations as $relation) {
117
                if(!isset($array[$relation])) {
118
                    $array[$relation] = (object) array('name' => $relation, 'relations' => array(), 'size' => 3000);
119
                }
120
//                array_push($array[$relation]->imports, $class);
121
            }
122
        }
123
        return array_values($array);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129
    public function getName() {
130
        return 'Summary HTML';
131
    }
132
}