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

AssetEngineSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 Psr\Log\LoggerInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
class AssetEngineSubscriber implements EventSubscriberInterface
22
{
23
    private $assetEngineManager;
24
    private $logger;
25
26 4
    public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger)
27
    {
28 4
        $this->assetEngineManager = $assetEngineManager;
29 4
        $this->logger = $logger;
30 4
    }
31
32 4
    public function processConfigurationSettings(ConfigurationParseComplete $event)
33
    {
34 4
        $configuration = $event->getConfiguration()->getConfiguration();
35
36
        /** @var AssetEngineInterface $engine */
37 4
        foreach ($this->assetEngineManager->getEngines() as $engine)
38
        {
39 4
            $defaults = __::get($configuration, $engine->getConfigurationNamespace(), []);
40 4
            $options = array_merge($engine->getDefaultConfiguration(), $defaults);
41
42 4
            $engine->setOptions($options);
43
        }
44 4
    }
45
46
    public function processAssetEnginePageView(PageManagerPostProcess $event)
47
    {
48
        /**
49
         * @var string               $folder
50
         * @var AssetEngineInterface $engine
51
         */
52
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
53
        {
54
            $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...
55
56
            if (!fs::exists($assetFolder))
57
            {
58
                continue;
59
            }
60
61
            $extensions = [];
62
63
            foreach ($engine->getExtensions() as $extension)
64
            {
65
                $extensions[] = "/.{$extension}.twig$/";
66
            }
67
68
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::IGNORE_DIRECTORIES);
69
70
            foreach ($explorer as $file)
71
            {
72
                $assetPageView = new StaticPageView($file);
73
                $compiled = $engine->parse($assetPageView->getContent());
74
                $assetPageView->setContent($compiled);
75
76
                try
77
                {
78
                    $event->getPageManager()->trackNewPageView($assetPageView);
79
                }
80
                catch (\Exception $e)
81
                {
82
                    $this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine');
83
                    $this->logger->error('  {message}', [
84
                        $e->getMessage(),
85
                    ]);
86
                }
87
            }
88
        }
89
    }
90
91
    public static function getSubscribedEvents()
92
    {
93
        return [
94
            ConfigurationParseComplete::NAME => 'processConfigurationSettings',
95
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
96
        ];
97
    }
98
}
99