Completed
Pull Request — master (#86)
by Vladimir
38:05 queued 09:07
created

compileAssetEnginePageViews()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\CompilerPostRenderStaticPageView;
15
use allejo\stakx\Event\ConfigurationParseComplete;
16
use allejo\stakx\Event\PageManagerPostProcess;
17
use allejo\stakx\Filesystem\FileExplorer;
18
use allejo\stakx\Filesystem\FilesystemLoader as fs;
19
use Psr\Log\LoggerInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
22
class AssetEngineSubscriber implements EventSubscriberInterface
23
{
24
    private $assetEngineManager;
25
    private $assetPageViews;
26
    private $logger;
27
28 7
    public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger)
29
    {
30 7
        $this->assetEngineManager = $assetEngineManager;
31 7
        $this->assetPageViews = [];
32 7
        $this->logger = $logger;
33 7
    }
34
35 7
    public function processConfigurationSettings(ConfigurationParseComplete $event)
36
    {
37 7
        $configuration = $event->getConfiguration()->getConfiguration();
38
39
        /** @var AssetEngineInterface $engine */
40 7
        foreach ($this->assetEngineManager->getEngines() as $engine)
41
        {
42 7
            $defaults = __::get($configuration, $engine->getConfigurationNamespace(), []);
43 7
            $options = array_merge($engine->getDefaultConfiguration(), $defaults);
44
45 7
            $engine->setOptions($options);
46
        }
47 7
    }
48
49 1
    public function processAssetEnginePageView(PageManagerPostProcess $event)
50
    {
51
        /**
52
         * @var string               $folder
53
         * @var AssetEngineInterface $engine
54
         */
55 1
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
56
        {
57 1
            $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...
58
59 1
            if (!fs::exists($assetFolder))
60
            {
61
                continue;
62
            }
63
64 1
            $engine->setPageManager($event->getPageManager());
65 1
            $extensions = [];
66
67 1
            foreach ($engine->getExtensions() as $extension)
68
            {
69 1
                $extensions[] = "/.{$extension}.twig$/";
70
            }
71
72 1
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::INCLUDE_ONLY_FILES | FileExplorer::IGNORE_DIRECTORIES);
73
74 1
            foreach ($explorer as $file)
75
            {
76 1
                $assetPageView = new StaticPageView($file);
77
78
                try
79
                {
80 1
                    $event->getPageManager()->trackNewPageView($assetPageView);
81 1
                    $this->assetPageViews[$assetPageView->getRelativeFilePath()] = [
82 1
                        'engine' => $engine,
83
                    ];
84
                }
85
                catch (\Exception $e)
86
                {
87
                    $this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine');
88
                    $this->logger->error('  {message}', [
89 1
                        'message' => $e->getMessage(),
90
                    ]);
91
                }
92
            }
93
        }
94 1
    }
95
96 1
    public function compileAssetEnginePageViews(CompilerPostRenderStaticPageView $event)
97
    {
98 1
        $pageView = $event->getPageView();
99 1
        $filePath = $pageView->getRelativeFilePath();
100
101 1
        if (isset($this->assetPageViews[$filePath]))
102
        {
103
            /** @var AssetEngineInterface $engine */
104 1
            $engine = $this->assetPageViews[$filePath]['engine'];
105
106 1
            $output = $engine->parse($event->getCompiledOutput(), [
107 1
                'pageview' => $pageView,
108
            ]);
109
110 1
            $event->setCompiledOutput($output);
111
        }
112 1
    }
113
114
    public static function getSubscribedEvents()
115
    {
116
        return [
117
            ConfigurationParseComplete::NAME => 'processConfigurationSettings',
118
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
119
            CompilerPostRenderStaticPageView::NAME => 'compileAssetEnginePageViews',
120
        ];
121
    }
122
}
123