Completed
Pull Request — master (#69)
by Vladimir
02:50
created

AssetEngineSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 66
c 0
b 0
f 0
wmc 9
lcom 1
cbo 10
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A processConfigurationSettings() 0 13 2
A processAssetEnginePageView() 0 34 5
A getSubscribedEvents() 0 7 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\EventSubscriber;
9
10
use __\__;
11
use allejo\stakx\AssetEngine\AssetEngineInterface;
12
use allejo\stakx\AssetEngine\AssetEngineManager;
13
use allejo\stakx\Document\StaticPageView;
14
use allejo\stakx\Event\ConfigurationParseComplete;
15
use allejo\stakx\Event\PageManagerPostProcess;
16
use allejo\stakx\Filesystem\FileExplorer;
17
use allejo\stakx\Filesystem\FilesystemLoader as fs;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
class AssetEngineSubscriber implements EventSubscriberInterface
21
{
22
    private $assetEngineManager;
23
24
    public function __construct(AssetEngineManager $assetEngineManager)
25
    {
26
        $this->assetEngineManager = $assetEngineManager;
27
    }
28
29
    public function processConfigurationSettings(ConfigurationParseComplete $event)
30
    {
31
        $configuration = $event->getConfiguration()->getConfiguration();
32
33
        /** @var AssetEngineInterface $engine */
34
        foreach ($this->assetEngineManager->getEngines() as $engine)
35
        {
36
            $defaults = __::get($configuration, $engine->getConfigurationNamespace(), []);
37
            $options = array_merge($engine->getDefaultConfiguration(), $defaults);
38
39
            $engine->setOptions($options);
40
        }
41
    }
42
43
    public function processAssetEnginePageView(PageManagerPostProcess $event)
44
    {
45
        /**
46
         * @var string               $folder
47
         * @var AssetEngineInterface $engine
48
         */
49
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
50
        {
51
            $assetFolder = fs::absolutePath($folder);
0 ignored issues
show
Documentation introduced by
$folder is of type integer|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...
52
53
            if (!fs::exists($assetFolder))
54
            {
55
                continue;
56
            }
57
58
            $extensions = [];
59
60
            foreach ($engine->getExtensions() as $extension)
61
            {
62
                $extensions[] = "/.{$extension}.twig$/";
63
            }
64
65
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::IGNORE_DIRECTORIES);
66
67
            foreach ($explorer as $file)
68
            {
69
                $assetPageView = new StaticPageView($file);
70
                $compiled = $engine->parse($assetPageView->getContent());
71
                $assetPageView->setContent($compiled);
72
73
                $event->getPageManager()->trackNewPageView($assetPageView);
74
            }
75
        }
76
    }
77
78
    public static function getSubscribedEvents()
79
    {
80
        return [
81
            ConfigurationParseComplete::NAME => 'processConfigurationSettings',
82
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
83
        ];
84
    }
85
}
86