Completed
Push — master ( 7bae76...0958d1 )
by Vladimir
9s
created

AssetEngineSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 11
dl 0
loc 82
ccs 28
cts 35
cp 0.8
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A processConfigurationSettings() 0 13 2
A getSubscribedEvents() 0 7 1
B processAssetEnginePageView() 0 48 6
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 7
    public function __construct(AssetEngineManager $assetEngineManager, LoggerInterface $logger)
27
    {
28 7
        $this->assetEngineManager = $assetEngineManager;
29 7
        $this->logger = $logger;
30 7
    }
31
32 7
    public function processConfigurationSettings(ConfigurationParseComplete $event)
33
    {
34 7
        $configuration = $event->getConfiguration()->getConfiguration();
35
36
        /** @var AssetEngineInterface $engine */
37 7
        foreach ($this->assetEngineManager->getEngines() as $engine)
38
        {
39 7
            $defaults = __::get($configuration, $engine->getConfigurationNamespace(), []);
40 7
            $options = array_merge($engine->getDefaultConfiguration(), $defaults);
41
42 7
            $engine->setOptions($options);
43
        }
44 7
    }
45
46 1
    public function processAssetEnginePageView(PageManagerPostProcess $event)
47
    {
48
        /**
49
         * @var string               $folder
50
         * @var AssetEngineInterface $engine
51
         */
52 1
        foreach ($this->assetEngineManager->getFoldersToWatch() as $folder => $engine)
53
        {
54 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...
55
56 1
            if (!fs::exists($assetFolder))
57
            {
58
                continue;
59
            }
60
61 1
            $engine->setPageManager($event->getPageManager());
62 1
            $extensions = [];
63
64 1
            foreach ($engine->getExtensions() as $extension)
65
            {
66 1
                $extensions[] = "/.{$extension}.twig$/";
67
            }
68
69 1
            $explorer = FileExplorer::create($assetFolder, [], $extensions, FileExplorer::IGNORE_DIRECTORIES);
70
71 1
            foreach ($explorer as $file)
72
            {
73 1
                $assetPageView = new StaticPageView($file);
74
75
                try
76
                {
77 1
                    $event->getPageManager()->trackNewPageView($assetPageView);
78
79 1
                    $compiledOutput = $engine->parse($assetPageView->getContent(), [
80 1
                        'pageview' => $assetPageView,
81
                    ]);
82 1
                    $assetPageView->setContent($compiledOutput);
83
                }
84
                catch (\Exception $e)
85
                {
86
                    $this->logger->error('An exception occurred while creating a Static PageView for an AssetEngine');
87
                    $this->logger->error('  {message}', [
88 1
                        'message' => $e->getMessage(),
89
                    ]);
90
                }
91
            }
92
        }
93 1
    }
94
95
    public static function getSubscribedEvents()
96
    {
97
        return [
98
            ConfigurationParseComplete::NAME => 'processConfigurationSettings',
99
            PageManagerPostProcess::NAME => 'processAssetEnginePageView',
100
        ];
101
    }
102
}
103