1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report\Html;
12
13use SebastianBergmann\CodeCoverage\Node\AbstractNode as Node;
14use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
15
16/**
17 * Renders a directory node.
18 */
19class Directory extends Renderer
20{
21    /**
22     * @param DirectoryNode $node
23     * @param string        $file
24     */
25    public function render(DirectoryNode $node, $file)
26    {
27        $template = new \Text_Template($this->templatePath . 'directory.html', '{{', '}}');
28
29        $this->setCommonTemplateVariables($template, $node);
30
31        $items = $this->renderItem($node, true);
32
33        foreach ($node->getDirectories() as $item) {
34            $items .= $this->renderItem($item);
35        }
36
37        foreach ($node->getFiles() as $item) {
38            $items .= $this->renderItem($item);
39        }
40
41        $template->setVar(
42            [
43                'id'    => $node->getId(),
44                'items' => $items
45            ]
46        );
47
48        $template->renderTo($file);
49    }
50
51    /**
52     * @param Node $node
53     * @param bool $total
54     *
55     * @return string
56     */
57    protected function renderItem(Node $node, $total = false)
58    {
59        $data = [
60            'numClasses'                   => $node->getNumClassesAndTraits(),
61            'numTestedClasses'             => $node->getNumTestedClassesAndTraits(),
62            'numMethods'                   => $node->getNumMethods(),
63            'numTestedMethods'             => $node->getNumTestedMethods(),
64            'linesExecutedPercent'         => $node->getLineExecutedPercent(false),
65            'linesExecutedPercentAsString' => $node->getLineExecutedPercent(),
66            'numExecutedLines'             => $node->getNumExecutedLines(),
67            'numExecutableLines'           => $node->getNumExecutableLines(),
68            'testedMethodsPercent'         => $node->getTestedMethodsPercent(false),
69            'testedMethodsPercentAsString' => $node->getTestedMethodsPercent(),
70            'testedClassesPercent'         => $node->getTestedClassesAndTraitsPercent(false),
71            'testedClassesPercentAsString' => $node->getTestedClassesAndTraitsPercent()
72        ];
73
74        if ($total) {
75            $data['name'] = 'Total';
76        } else {
77            if ($node instanceof DirectoryNode) {
78                $data['name'] = sprintf(
79                    '<a href="%s/index.html">%s</a>',
80                    $node->getName(),
81                    $node->getName()
82                );
83
84                $data['icon'] = '<span class="glyphicon glyphicon-folder-open"></span> ';
85            } else {
86                $data['name'] = sprintf(
87                    '<a href="%s.html">%s</a>',
88                    $node->getName(),
89                    $node->getName()
90                );
91
92                $data['icon'] = '<span class="glyphicon glyphicon-file"></span> ';
93            }
94        }
95
96        return $this->renderItemTemplate(
97            new \Text_Template($this->templatePath . 'directory_item.html', '{{', '}}'),
98            $data
99        );
100    }
101}
102