SculpinApplication   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 91.49%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 0
loc 129
ccs 43
cts 47
cp 0.9149
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A runCommand() 0 15 2
A processCommand() 0 23 1
A loadConfigurationWithDirectories() 0 5 1
A loadSourcesFromSourceDirectory() 0 5 1
A findFilesInSourceDirectory() 0 10 1
A loadLayoutsToLatteLoader() 0 8 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\Application;
11
12
use Nette\Utils\Finder;
13
use SplFileInfo;
14
use Symplify\PHP7_Sculpin\Application\Command\RunCommand;
15
use Symplify\PHP7_Sculpin\Configuration\Configuration;
16
use Symplify\PHP7_Sculpin\HttpServer\HttpServer;
17
use Symplify\PHP7_Sculpin\Output\FileSystemWriter;
18
use Symplify\PHP7_Sculpin\Renderable\Latte\DynamicStringLoader;
19
use Symplify\PHP7_Sculpin\Renderable\RenderableFilesProcessor;
20
use Symplify\PHP7_Sculpin\Source\SourceFileStorage;
21
use Symplify\PHP7_Sculpin\Utils\FilesystemChecker;
22
23
final class SculpinApplication
24
{
25
    /**
26
     * @var SourceFileStorage
27
     */
28
    private $sourceFileStorage;
29
30
    /**
31
     * @var Configuration
32
     */
33
    private $configuration;
34
35
    /**
36
     * @var FileSystemWriter
37
     */
38
    private $fileSystemWriter;
39
40
    /**
41
     * @var RenderableFilesProcessor
42
     */
43
    private $renderableFilesProcessor;
44
45
    /**
46
     * @var DynamicStringLoader
47
     */
48
    private $dynamicStringLoader;
49
50
    /**
51
     * @var HttpServer
52
     */
53
    private $httpServer;
54
55
    /**
56
     * @var string
57
     */
58
    private $sinceTime;
59
60 4
    public function __construct(
61
        SourceFileStorage $sourceFileStorage,
62
        Configuration $configuration,
63
        FileSystemWriter $fileSystemWriter,
64
        RenderableFilesProcessor $renderableFilesProcessor,
65
        DynamicStringLoader $dynamicStringLoader,
66
        HttpServer $httpServer
67
    ) {
68 4
        $this->sourceFileStorage = $sourceFileStorage;
69 4
        $this->configuration = $configuration;
70 4
        $this->fileSystemWriter = $fileSystemWriter;
71 4
        $this->renderableFilesProcessor = $renderableFilesProcessor;
72 4
        $this->dynamicStringLoader = $dynamicStringLoader;
73 4
        $this->httpServer = $httpServer;
74 4
        $this->sinceTime = '1970-01-01T00:00:00Z';
75 4
    }
76
77 4
    public function runCommand(RunCommand $runCommand)
78
    {
79 4
        $this->processCommand($runCommand);
80
81 1
        if ($runCommand->isRunServer()) {
82
            $this->httpServer->init();
83
84
            $this->httpServer->addPeriodicTimer(1, function () use ($runCommand) {
85
                clearstatcache();
86
                $this->processCommand($runCommand);
87
            });
88
89
            $this->httpServer->run();
90
        }
91 1
    }
92
93 4
    private function processCommand(RunCommand $runCommand)
94
    {
95 4
        $this->loadConfigurationWithDirectories($runCommand);
96
97 4
        FilesystemChecker::ensureDirectoryExists($runCommand->getSourceDirectory());
98
99 1
        $this->loadSourcesFromSourceDirectory($runCommand->getSourceDirectory());
100
101
        // 1. copy static files
102 1
        $this->fileSystemWriter->copyStaticFiles($this->sourceFileStorage->getStaticFiles());
103
104
        // 2. collect configuration
105 1
        $this->configuration->loadFromFiles($this->sourceFileStorage->getConfigurationFiles());
106
107
        // 3. collect layouts
108 1
        $this->loadLayoutsToLatteLoader($this->sourceFileStorage->getLayoutFiles());
109
110
        // 4. completely process post
111 1
        $this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getPostFiles());
112
113
        // 5. render files
114 1
        $this->renderableFilesProcessor->processFiles($this->sourceFileStorage->getRenderableFiles());
115 1
    }
116
117 4
    private function loadConfigurationWithDirectories(RunCommand $runCommand)
118
    {
119 4
        $this->configuration->setSourceDirectory($runCommand->getSourceDirectory());
120 4
        $this->configuration->setOutputDirectory($runCommand->getOutputDirectory());
121 4
    }
122
123 1
    private function loadSourcesFromSourceDirectory(string $sourceDirectory)
124
    {
125 1
        $finder = $this->findFilesInSourceDirectory($sourceDirectory);
126 1
        $this->sourceFileStorage->loadSourcesFromFinder($finder);
127 1
    }
128
129 1
    private function findFilesInSourceDirectory(string $sourceDirectory) : Finder
130
    {
131 1
        $sinceTimeLast = $this->sinceTime;
132
133 1
        $this->sinceTime = date('c');
134
135 1
        return Finder::findFiles('*')
136 1
            ->from($sourceDirectory)
137 1
            ->date('>=', $sinceTimeLast);
138
    }
139
140
    /**
141
     * @param SplFileInfo[] $layoutFiles
142
     */
143 1
    private function loadLayoutsToLatteLoader(array $layoutFiles)
144
    {
145 1
        foreach ($layoutFiles as $layoutFile) {
146 1
            $name = $layoutFile->getBasename('.' . $layoutFile->getExtension());
147 1
            $content = file_get_contents($layoutFile->getRealPath());
148 1
            $this->dynamicStringLoader->addTemplate($name, $content);
149
        }
150 1
    }
151
}
152