Completed
Pull Request — master (#68)
by Vladimir
03:57
created

Website::watchListenerFunction()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 16
nop 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx;
9
10
use allejo\stakx\Event\BuildProcessComplete;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\Filesystem\Folder;
13
use allejo\stakx\Manager\AssetManager;
14
use allejo\stakx\Manager\ThemeManager;
15
use allejo\stakx\Templating\TemplateBridgeInterface;
16
use Highlight\Highlighter;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
class Website
20
{
21
    private $configuration;
22
    private $logger;
23
    private $templateBridge;
24
    private $compiler;
25
    private $assetManager;
26
    private $eventDispatcher;
27
28 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
        Compiler $compiler,
30
        Configuration $configuration,
31
        AssetManager $assetManager,
32
        TemplateBridgeInterface $templateBridge,
33
        EventDispatcherInterface $eventDispatcher,
34
        Logger $logger
35
    ) {
36
        $this->configuration = $configuration;
37
        $this->logger = $logger;
38
        $this->templateBridge = $templateBridge;
39
        $this->compiler = $compiler;
40
        $this->assetManager = $assetManager;
41
        $this->eventDispatcher = $eventDispatcher;
42
    }
43
44
    /**
45
     * Compile the website.
46
     *
47
     * @return true if the website built successfully
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    public function build()
50
    {
51
        if (empty($this->getConfiguration()->getPageViewFolders()))
52
        {
53
            $this->logger->error('No PageViews were configured for this site. Check the `pageviews` key in your _config.yml.');
54
55
            return false;
56
        }
57
58
        // Configure the environment
59
        $this->createFolderStructure();
60
        $this->configureHighlighter();
61
62
        // Our output directory
63
        $outputDirectory = new Folder($this->getConfiguration()->getTargetFolder());
64
        $outputDirectory->setTargetDirectory($this->getConfiguration()->getBaseUrl());
65
66
        // Compile everything
67
        $theme = $this->getConfiguration()->getTheme();
68
69
        $this->compiler->setTargetFolder($outputDirectory);
70
        $this->compiler->setThemeName($theme);
71
        $this->compiler->compileAll();
72
73
        if (Service::hasRunTimeFlag(RuntimeStatus::IN_PROFILE_MODE))
74
        {
75
            if (!$this->templateBridge->hasProfiler())
76
            {
77
                $this->logger->writeln('This template engine currently does not support a profiler.');
78
            }
79
            else
80
            {
81
                $profilerText = $this->templateBridge->getProfilerOutput($this->compiler);
82
                $this->logger->writeln($profilerText);
83
            }
84
        }
85
86
        // At this point, we are looking at static files to copy over meaning we need to ignore all of the files that
87
        // make up the source of a stakx website
88
        $assetsToIgnore = array_merge(
89
            Configuration::$stakxSourceFiles,
90
            $this->getConfiguration()->getExcludes()
91
        );
92
93
        //
94
        // Theme Management
95
        //
96
        if ($theme !== null)
97
        {
98
            $this->logger->notice("Looking for '${theme}' theme...");
99
100
            $tm = new ThemeManager($theme, $this->eventDispatcher, $this->logger);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $tm. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
101
            $tm->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore);
102
            $tm->setFolder($outputDirectory);
103
            $tm->copyFiles();
104
        }
105
106
        //
107
        // Static file management
108
        //
109
        $this->assetManager->configureFinder($this->getConfiguration()->getIncludes(), $assetsToIgnore);
110
        $this->assetManager->setFolder($outputDirectory);
111
        $this->assetManager->copyFiles();
112
113
        $this->eventDispatcher->dispatch(BuildProcessComplete::NAME, new BuildProcessComplete());
114
    }
115
116
    /**
117
     * @return Configuration
118
     */
119
    public function getConfiguration()
120
    {
121
        return $this->configuration;
122
    }
123
124
    /**
125
     * Prepare the Stakx environment by creating necessary cache folders.
126
     */
127
    private function createFolderStructure()
128
    {
129
        $targetDir = fs::absolutePath($this->getConfiguration()->getTargetFolder());
0 ignored issues
show
Documentation introduced by
$this->getConfiguration()->getTargetFolder() is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
131
        if (!Service::hasRunTimeFlag(RuntimeStatus::BOOT_WITHOUT_CLEAN))
132
        {
133
            fs::remove($targetDir);
0 ignored issues
show
Bug introduced by
The method remove() does not exist on allejo\stakx\Filesystem\FilesystemLoader. Did you maybe mean removeExtension()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
134
        }
135
136
        if (!Service::hasRunTimeFlag(RuntimeStatus::USING_CACHE))
137
        {
138
            fs::remove(fs::absolutePath(Configuration::CACHE_FOLDER, 'twig'));
0 ignored issues
show
Unused Code introduced by
The call to FilesystemLoader::absolutePath() has too many arguments starting with 'twig'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Bug introduced by
The method remove() does not exist on allejo\stakx\Filesystem\FilesystemLoader. Did you maybe mean removeExtension()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
139
            fs::mkdir(fs::absolutePath(fs::appendPath(Configuration::CACHE_FOLDER, 'twig')));
140
        }
141
142
        fs::mkdir($targetDir);
143
    }
144
145
    /**
146
     * Configure the Highlighter object for highlighting code blocks.
147
     */
148
    private function configureHighlighter()
149
    {
150
        $enabled = $this->getConfiguration()->isHighlighterEnabled();
151
152
        if (!$enabled)
153
        {
154
            return;
155
        }
156
157
        Service::setRuntimeFlag(RuntimeStatus::USING_HIGHLIGHTER);
158
159
        foreach ($this->getConfiguration()->getHighlighterCustomLanguages() as $lang => $path)
160
        {
161
            $fullPath = fs::absolutePath($path);
162
163
            if (!fs::exists($fullPath))
164
            {
165
                $this->logger->warning('The following language definition could not be found: {lang}', [
166
                    'lang' => $path,
167
                ]);
168
                continue;
169
            }
170
171
            Highlighter::registerLanguage($lang, $fullPath);
172
            $this->logger->debug('Loading custom language {lang} from {path}...', [
173
                'lang' => $lang,
174
                'path' => $path,
175
            ]);
176
        }
177
    }
178
}
179