Completed
Push — master ( 434e76...71c234 )
by Demonchaux
02:02
created

Render::render()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 8.7624
cc 5
eloc 11
nc 12
nop 3
crap 30
1
<?php
2
3
/**
4
 * This file is part of the Clover to Html package.
5
 *
6
 * (c) Stéphane Demonchaux <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace CloverToHtml;
12
13
class Render
14
{
15
    /**
16
     * @var TwigCreator
17
     */
18
    private $twigCreator;
19
    /**
20
     * @var ConfigDAO
21
     */
22
    private $configDAO;
23
24
    /**
25
     * Construct.
26
     *
27
     * @param TwigCreator $twig
28
     * @param ConfigDAO   $configDAO
29
     */
30 4
    public function __construct(TwigCreator $twig, ConfigDAO $configDAO)
31
    {
32 4
        $this->twigCreator = $twig;
33 4
        $this->configDAO   = $configDAO;
34 4
    }
35
36
    /**
37
     * @param Root   $root
38
     * @param string $target
39
     * @param string $templatePath
40
     */
41
    public function render(Root $root, $target, $templatePath = false)
42
    {
43
        if ($templatePath === false) {
44
            $templatePath = __DIR__.'/Template/';
45
        }
46
47
        $twig = $this->twigCreator->createInstance($templatePath);
48
49
        foreach ($root->getFileCollection() as $file) {
50
            $this->renderFile($file, $twig, $target, $root->getBasePath());
51
        }
52
53
        foreach ($root->getDirectoryCollection() as $directory) {
54
            $this->renderDirectory($directory, $root, $twig, $target);
55
            foreach ($directory->getFileCollection() as $file) {
56
                $this->renderFile($file, $twig, $target, $root->getBasePath());
57
            }
58
        }
59
60
        $this->copyAssets($templatePath, $target);
61
    }
62
63
    /**
64
     * @param string $templatePath
65
     * @param string $target
66
     */
67
    private function copyAssets($templatePath, $target)
68
    {
69
        try {
70
            $files = $this->configDAO->findConfig($templatePath, 'files');
71
            foreach ($files as $file) {
72
                $this->createDirIfNotExist($target.'/'.$file);
73
                copy($templatePath.$file, $target.'/'.$file);
74
            }
75
        } catch (\InvalidArgumentException $e) {
76
            // Nothing to do
77
        }
78
    }
79
80
    /**
81
     * @param string $path
82
     */
83
    private function createDirIfNotExist($path)
84
    {
85
        if (is_dir(dirname($path)) === false) {
86
            mkdir(dirname($path), 0777, true);
87
        }
88
    }
89
90
    /**
91
     * @param string $base
92
     * @param string $actual
93
     *
94
     * @return string
95
     */
96
    private function assetsPath($base, $actual)
97
    {
98
        return str_repeat('../', (substr_count($actual, '/') - substr_count($base, '/')) - 1);
99
    }
100
101
    /**
102
     * @param File              $file
103
     * @param \Twig_Environment $twig
104
     * @param string            $target
105
     * @param string            $basePath
106
     */
107
    private function renderFile(File $file, \Twig_Environment $twig, $target, $basePath)
108
    {
109
        $path = $target.'/'.$file->getDestination($basePath);
110
111
        $this->createDirIfNotExist($path);
112
113
        file_put_contents(
114
            $path,
115
            $twig->render(
116
                'file.twig',
117
                array(
118
                    'file' => $file,
119
                    'assets' => $this->assetsPath($target, $path),
120
                )
121
            )
122
         );
123
    }
124
125
    /**
126
     * @param Directory         $directory
127
     * @param Root              $root
128
     * @param \Twig_Environment $twig
129
     * @param string            $target
130
     */
131
    private function renderDirectory(Directory $directory, Root $root, \Twig_Environment $twig, $target)
132
    {
133
        $path = $target.'/'.$directory->getDestination();
134
135
        $this->createDirIfNotExist($path);
136
137
        file_put_contents(
138
            $path,
139
            $twig->render(
140
                'directory.twig',
141
                array(
142
                    'directory' => $directory,
143
                    'directoryCollection' => $root->getAllDirIn($directory),
144
                    'assets' => $this->assetsPath($target, $path),
145
                )
146
            )
147
        );
148
    }
149
}
150