Completed
Push — master ( 2386c7...c56f94 )
by Tomáš
02:42
created

RenderableFilesProcessor::processFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Renderable;
11
12
use SplFileInfo;
13
use Symplify\PHP7_Sculpin\Output\FileSystemWriter;
14
use Symplify\PHP7_Sculpin\Configuration\Configuration;
15
use Symplify\PHP7_Sculpin\Renderable\Configuration\ConfigurationDecorator;
16
use Symplify\PHP7_Sculpin\Renderable\File\File;
17
use Symplify\PHP7_Sculpin\Renderable\File\FileFactory;
18
use Symplify\PHP7_Sculpin\Renderable\File\PostFile;
19
use Symplify\PHP7_Sculpin\Renderable\Latte\LatteDecorator;
20
use Symplify\PHP7_Sculpin\Renderable\Markdown\MarkdownDecorator;
21
use Symplify\PHP7_Sculpin\Renderable\Routing\RouteDecorator;
22
23
final class RenderableFilesProcessor
24
{
25
    /**
26
     * @var FileFactory
27
     */
28
    private $fileFactory;
29
30
    /**
31
     * @var RouteDecorator
32
     */
33
    private $routeDecorator;
34
35
    /**
36
     * @var ConfigurationDecorator
37
     */
38
    private $configurationDecorator;
39
40
    /**
41
     * @var MarkdownDecorator
42
     */
43
    private $markdownDecorator;
44
    /**
45
     * @var LatteDecorator
46
     */
47
    private $latteDecorator;
48
49
    /**
50
     * @var FileSystemWriter
51
     */
52
    private $fileSystemWriter;
53
54
    /**
55
     * @var Configuration
56
     */
57
    private $configuration;
58
59 4
    public function __construct(
60
        FileFactory $fileFactory,
61
        RouteDecorator $routeDecorator,
62
        ConfigurationDecorator $configurationDecorator,
63
        MarkdownDecorator $markdownDecorator,
64
        LatteDecorator $latteDecorator,
65
        FileSystemWriter $fileSystemWriter,
66
        Configuration $configuration
67
    ) {
68 4
        $this->fileFactory = $fileFactory;
69 4
        $this->routeDecorator = $routeDecorator;
70 4
        $this->configurationDecorator = $configurationDecorator;
71 4
        $this->markdownDecorator = $markdownDecorator;
72 4
        $this->latteDecorator = $latteDecorator;
73 4
        $this->fileSystemWriter = $fileSystemWriter;
74 4
        $this->configuration = $configuration;
75 4
    }
76
77
    /**
78
     * @param SplFileInfo[] $fileInfos
79
     */
80 4
    public function processFiles(array $fileInfos)
81
    {
82 4
        if (!count($fileInfos)) {
83 2
            return;
84
        }
85
86 4
        $files = $this->createFileObjectsFromFileInfos($fileInfos);
87
88 4
        $this->setPostsToConfiguration($files);
89 4
        $this->setRoutesToFiles($files);
90
91 4
        $this->setFileConfigurationToFile($files);
92 4
        $this->formatFileContentFromMarkdownToHtml($files);
93 4
        $this->formatFileContentFromLatteToHtml($files);
94
95 4
        $this->fileSystemWriter->copyRenderableFiles($files);
96 4
    }
97
98
    /**
99
     * @param array $fileInfos
100
     *
101
     * @return array
102
     */
103 4
    private function createFileObjectsFromFileInfos(array $fileInfos):array
104
    {
105 4
        $files = [];
106 4
        foreach ($fileInfos as $id => $fileInfo) {
107 4
            $files[$id] = $this->fileFactory->create($fileInfo);
108
        }
109
110 4
        return $files;
111
    }
112
113
    /**
114
     * @param File[] $files
115
     */
116 4
    private function setPostsToConfiguration(array $files)
117
    {
118 4
        if (reset($files) instanceof PostFile) {
119 1
            $this->configuration->addOption('posts', $files);
120
        }
121 4
    }
122
123
    /**
124
     * @param File[] $files
125
     */
126 4
    private function setRoutesToFiles(array $files)
127
    {
128 4
        foreach ($files as $file) {
129 4
            $this->routeDecorator->decorateFile($file);
130
        }
131 4
    }
132
133
    /**
134
     * @param File[] $files
135
     */
136 4
    private function setFileConfigurationToFile(array $files)
137
    {
138 4
        foreach ($files as $file) {
139 4
            $this->configurationDecorator->decorateFile($file);
140
        }
141 4
    }
142
143
    /**
144
     * @param File[] $files
145
     */
146 4
    private function formatFileContentFromMarkdownToHtml(array $files)
147
    {
148 4
        foreach ($files as $file) {
149 4
            $this->markdownDecorator->decorateFile($file);
150
        }
151 4
    }
152
153
    /**
154
     * @param File[] $files
155
     */
156 4
    private function formatFileContentFromLatteToHtml(array $files)
157
    {
158 4
        foreach ($files as $file) {
159 4
            $this->latteDecorator->decorateFile($file);
160
        }
161 4
    }
162
}
163