Completed
Pull Request — master (#91)
by Vladimir
05:42
created

AssetEngineSubscriber   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 79.41%

Importance

Changes 0
Metric Value
dl 0
loc 124
c 0
b 0
f 0
wmc 14
lcom 1
cbo 15
rs 10
ccs 27
cts 34
cp 0.7941

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A processConfigurationSettings() 0 13 2
B processAssetEnginePageView() 0 46 6
A compileAssetEnginePageViews() 0 24 3
A getSubscribedEvents() 0 8 1
A buildCacheFolder() 0 15 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\Configuration;
14
use allejo\stakx\Document\StaticPageView;
15
use allejo\stakx\Event\CompilerPostRenderStaticPageView;
16
use allejo\stakx\Event\ConfigurationParseComplete;
17
use allejo\stakx\Event\PageManagerPostProcess;
18
use allejo\stakx\Filesystem\FileExplorer;
19
use allejo\stakx\Filesystem\FilesystemLoader as fs;
20
use allejo\stakx\Filesystem\FilesystemPath;
21
use allejo\stakx\Filesystem\Folder;
22
use allejo\stakx\RuntimeStatus;
23
use allejo\stakx\Service;
24
use Psr\Log\LoggerInterface;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26 7
27
class AssetEngineSubscriber implements EventSubscriberInterface
28 7
{
29 7
    private $assetEngineManager;
30 7
    private $assetPageViews;
31
    private $logger;
32 7
33
    public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger)
34 7
    {
35
        $this->assetEngineManager = $assetEngineManager;
36
        $this->assetPageViews = [];
37 7
        $this->logger = $logger;
38
    }
39 7
40 7
    public function processConfigurationSettings(ConfigurationParseComplete $event)
41
    {
42 7
        $configuration = $event->getConfiguration()->getConfiguration();
43
44 7
        /** @var AssetEngineInterface $engine */
45
        foreach ($this->assetEngineManager->getEngines() as $engine)
46 1
        {
47
            $defaults = __::get($configuration, $engine->getConfigurationNamespace(), []);
48
            $options = array_merge($engine->getDefaultConfiguration(), $defaults);
49
50
            $engine->setOptions($options);
51
        }
52 1
    }
53
54 1
    public function processAssetEnginePageView(PageManagerPostProcess $event)
55
    {
56 1
        /**
57
         * @var string               $folder
58
         * @var AssetEngineInterface $engine
59
         */
60
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
61 1
        {
62 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...
63
64 1
            if (!fs::exists($assetFolder))
65
            {
66 1
                continue;
67
            }
68
69 1
            $engine->setPageManager($event->getPageManager());
70
            $extensions = [];
71 1
72
            foreach ($engine->getExtensions() as $extension)
73 1
            {
74
                $extensions[] = "/.{$extension}.twig$/";
75
            }
76
77 1
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::INCLUDE_ONLY_FILES | FileExplorer::IGNORE_DIRECTORIES);
78
79 1
            foreach ($explorer as $file)
80 1
            {
81
                $assetPageView = new StaticPageView($file);
82 1
83
                try
84
                {
85
                    $event->getPageManager()->trackNewPageView($assetPageView);
86
                    $this->assetPageViews[$assetPageView->getRelativeFilePath()] = [
87
                        'engine' => $engine,
88 1
                    ];
89
                }
90
                catch (\Exception $e)
91
                {
92
                    $this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine');
93 1
                    $this->logger->error('  {message}', [
94
                        'message' => $e->getMessage(),
95
                    ]);
96
                }
97
            }
98
        }
99
    }
100
101
    public function compileAssetEnginePageViews(CompilerPostRenderStaticPageView $event)
102
    {
103
        $pageView = $event->getPageView();
104
        $filePath = $pageView->getRelativeFilePath();
105
106
        if (isset($this->assetPageViews[$filePath]))
107
        {
108
            /** @var AssetEngineInterface $engine */
109
            $engine = $this->assetPageViews[$filePath]['engine'];
110
            $cacheDir = $this->buildCacheFolder($engine);
111
112
            if (Service::hasRunTimeFlag(RuntimeStatus::USING_CACHE))
113
            {
114
                $engine->loadCache($cacheDir);
115
            }
116
117
            $output = $engine->parse($event->getCompiledOutput(), [
118
                'pageview' => $pageView,
119
            ]);
120
121
            $engine->saveCache($cacheDir);
122
            $event->setCompiledOutput($output);
123
        }
124
    }
125
126
    public static function getSubscribedEvents()
127
    {
128
        return [
129
            ConfigurationParseComplete::NAME => 'processConfigurationSettings',
130
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
131
            CompilerPostRenderStaticPageView::NAME => 'compileAssetEnginePageViews',
132
        ];
133
    }
134
135
    private function buildCacheFolder(AssetEngineInterface $engine)
136
    {
137
        $cacheDirPath = new FilesystemPath(Service::getWorkingDirectory() . '/');
138
        $cacheDirPath
139
            ->appendToPath(Configuration::CACHE_FOLDER . '/')
140
            ->appendToPath(__::slug(strtolower($engine->getName())) . '/')
141
        ;
142
143
        fs::mkdir($cacheDirPath);
144
145
        $cacheDir = new Folder($cacheDirPath);
146
        $cacheDir->freeze();
147
148
        return $cacheDir;
149
    }
150
}
151