Issues (8)

src/Report/HtmlReportGenerator.php (1 issue)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scheb\Tombstone\Analyzer\Report;
6
7
use Scheb\Tombstone\Analyzer\AnalyzerResult;
8
use Scheb\Tombstone\Analyzer\Report\Html\Exception\HtmlReportException;
9
use Scheb\Tombstone\Analyzer\Report\Html\Renderer\DashboardRenderer;
10
use Scheb\Tombstone\Analyzer\Report\Html\Renderer\DirectoryRenderer;
11
use Scheb\Tombstone\Analyzer\Report\Html\Renderer\FileRenderer;
12
13
class HtmlReportGenerator implements ReportGeneratorInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $reportDir;
19
20
    /**
21
     * @var string
22
     */
23
    private $rootDir;
24
25
    /**
26
     * @var string
27
     */
28
    private $templateDir;
29
30
    public function __construct(string $reportDir, string $rootDir)
31
    {
32
        $this->reportDir = $reportDir;
33
        $this->rootDir = $rootDir;
34
        $this->templateDir = __DIR__.'/Html/Template';
35
    }
36
37
    public function getName(): string
38
    {
39
        return 'HTML';
40
    }
41
42
    public function generate(AnalyzerResult $result): void
43
    {
44
        $this->copySkeleton();
45
46
        $dashboardRenderer = new DashboardRenderer($this->reportDir, $this->rootDir);
47
        $dashboardRenderer->generate($result);
48
49
        $directoryRenderer = new DirectoryRenderer($this->reportDir, $this->rootDir);
50
        $directoryRenderer->generate($result);
51
52
        $fileRenderer = new FileRenderer($this->reportDir, $this->rootDir);
53
        $fileRenderer->generate($result);
54
    }
55
56
    private function copySkeleton(): void
57
    {
58
        $this->createDirectory($this->reportDir);
59
        $this->copyDirectoryFiles($this->templateDir.DIRECTORY_SEPARATOR.'css', $this->reportDir.DIRECTORY_SEPARATOR.'css');
60
        $this->copyDirectoryFiles($this->templateDir.DIRECTORY_SEPARATOR.'icons', $this->reportDir.DIRECTORY_SEPARATOR.'icons');
61
        $this->copyDirectoryFiles($this->templateDir.DIRECTORY_SEPARATOR.'img', $this->reportDir.DIRECTORY_SEPARATOR.'img');
62
    }
63
64
    private function copyDirectoryFiles(string $templateDir, string $reportDir): void
65
    {
66
        $this->createDirectory($reportDir);
67
        $handle = opendir($templateDir);
68
        if (!$handle) {
0 ignored issues
show
$handle is of type false|resource, thus it always evaluated to false.
Loading history...
69
            throw new HtmlReportException(sprintf('Could not read template files from %s', $templateDir));
70
        }
71
72
        while ($file = readdir($handle)) {
73
            if ('.' === $file || '..' === $file) {
74
                continue;
75
            }
76
77
            $templateFile = $templateDir.DIRECTORY_SEPARATOR.$file;
78
            $reportFile = $reportDir.DIRECTORY_SEPARATOR.$file;
79
80
            if (is_dir($templateFile)) {
81
                $this->copyDirectoryFiles($templateFile, $reportFile);
82
                continue;
83
            }
84
85
            if ([email protected]($templateFile, $reportFile)) {
86
                throw new HtmlReportException(sprintf('Could not copy %s to %s', $templateFile, $reportFile));
87
            }
88
        }
89
        closedir($handle);
90
    }
91
92
    private function createDirectory(string $dir): void
93
    {
94
        if (!is_dir($dir)) {
95
            if ([email protected]($dir, 0777, true)) {
96
                throw new HtmlReportException(sprintf('Could not create directory %s', $dir));
97
            }
98
        } elseif (!is_writable($dir)) {
99
            throw new HtmlReportException(sprintf('Directory %s has to be writable', $dir));
100
        }
101
    }
102
}
103