Completed
Push — master ( 54c8d8...9da0fd )
by Tomáš
9s
created

formatFileContentFromMarkdownToHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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 1
    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 1
        $this->fileFactory = $fileFactory;
69 1
        $this->routeDecorator = $routeDecorator;
70 1
        $this->configurationDecorator = $configurationDecorator;
71 1
        $this->markdownDecorator = $markdownDecorator;
72 1
        $this->latteDecorator = $latteDecorator;
73 1
        $this->fileSystemWriter = $fileSystemWriter;
74 1
        $this->configuration = $configuration;
75 1
    }
76
77
    /**
78
     * @param SplFileInfo[] $fileInfos
79
     */
80 1
    public function processFiles(array $fileInfos)
81
    {
82 1
        if (!count($fileInfos)) {
83
            return;
84
        }
85
86 1
        $files = $this->createFileObjectsFromFileInfos($fileInfos);
87
88 1
        $this->setPostsToConfiguration($files);
89 1
        $this->setRoutesToFiles($files);
90
91 1
        $this->setFileConfigurationToFile($files);
92 1
        $this->formatFileContentFromMarkdownToHtml($files);
93 1
        $this->formatFileContentFromLatteToHtml($files);
94
95 1
        $this->fileSystemWriter->copyRenderableFiles($files);
96 1
    }
97
98
    /**
99
     * @param array $fileInfos
100
     *
101
     * @return array
102
     */
103 1
    private function createFileObjectsFromFileInfos(array $fileInfos):array
104
    {
105 1
        $files = [];
106 1
        foreach ($fileInfos as $id => $fileInfo) {
107 1
            $files[$id] = $this->fileFactory->create($fileInfo);
108
        }
109
110 1
        return $files;
111
    }
112
113
    /**
114
     * @param File[] $files
115
     */
116 1
    private function setPostsToConfiguration(array $files)
117
    {
118 1
        if (reset($files) instanceof PostFile) {
119
            $this->configuration->addOption('posts', $files);
120
        }
121 1
    }
122
123
    /**
124
     * @param File[] $files
125
     */
126 1
    private function setRoutesToFiles(array $files)
127
    {
128 1
        foreach ($files as $file) {
129 1
            $this->routeDecorator->decorateFile($file);
130
        }
131 1
    }
132
133
    /**
134
     * @param File[] $files
135
     */
136 1
    private function setFileConfigurationToFile(array $files)
137
    {
138 1
        foreach ($files as $file) {
139 1
            $this->configurationDecorator->decorateFile($file);
140
        }
141 1
    }
142
143
    /**
144
     * @param File[] $files
145
     */
146 1
    private function formatFileContentFromMarkdownToHtml(array $files)
147
    {
148 1
        foreach ($files as $file) {
149 1
            $this->markdownDecorator->decorateFile($file);
150
        }
151 1
    }
152
153
    /**
154
     * @param File[] $files
155
     */
156 1
    private function formatFileContentFromLatteToHtml(array $files)
157
    {
158 1
        foreach ($files as $file) {
159 1
            $this->latteDecorator->decorateFile($file);
160
        }
161 1
    }
162
}
163