Completed
Push — master ( d42705...573ee7 )
by Tomáš
05:03 queued 02:39
created

createFileObjectsFromFileInfos()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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