Listener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 26
dl 0
loc 61
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 11 1
A renderAssets() 0 37 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fabiang\AsseticBundle;
6
7
use Laminas\EventManager\AbstractListenerAggregate;
8
use Laminas\EventManager\EventManagerInterface;
9
use Laminas\Http\PhpEnvironment\Response;
10
use Laminas\Mvc\MvcEvent;
11
use Laminas\Stdlib\ResponseInterface;
12
13
use function in_array;
14
15
class Listener extends AbstractListenerAggregate
16
{
17
    /**
18
     * Attach one or more listeners
19
     *
20
     * Implementors may add an optional $priority argument; the EventManager
21 1
     * implementation will pass this to the aggregate.
22
     *
23 1
     * @param int $priority
24
     */
25
    public function attach(EventManagerInterface $events, $priority = 32): void
26
    {
27
        $this->listeners[] = $events->attach(
28 1
            MvcEvent::EVENT_DISPATCH,
29
            [$this, 'renderAssets'],
30
            $priority
31
        );
32
        $this->listeners[] = $events->attach(
33
            MvcEvent::EVENT_DISPATCH_ERROR,
34
            [$this, 'renderAssets'],
35 2
            $priority
36
        );
37 2
    }
38
39
    public function renderAssets(MvcEvent $e): void
40 2
    {
41 2
        $sm = $e->getApplication()->getServiceManager();
42 1
43 1
        /** @var Configuration $config */
44
        $config = $sm->get('AsseticConfiguration');
45 1
        if ($e->getName() === MvcEvent::EVENT_DISPATCH_ERROR) {
46
            $error = $e->getError();
47
            if ($error && ! in_array($error, $config->getAcceptableErrors())) {
48
                // break if not an acceptable error
49
                return;
50 1
            }
51 1
        }
52 1
53 1
        /** @var ResponseInterface|null $response */
54
        $response = $e->getResponse();
55
        if (! $response) {
0 ignored issues
show
introduced by
$response is of type Laminas\Stdlib\ResponseInterface, thus it always evaluated to true.
Loading history...
56
            $response = new Response();
57 1
            $e->setResponse($response);
58
        }
59
60 1
        /** @var Service $asseticService */
61 1
        $asseticService = $sm->get('AsseticService');
62 1
63 1
        // setup service if a matched route exist
64 1
        $router = $e->getRouteMatch();
65
        if ($router) {
66
            $asseticService->setRouteName($router->getMatchedRouteName());
67
            $asseticService->setControllerName($router->getParam('controller'));
68 1
            $asseticService->setActionName($router->getParam('action'));
69
        }
70
71 1
        // Create all objects
72
        $asseticService->build();
73
74
        // Init assets for modules
75
        $asseticService->setupRenderer($sm->get('ViewRenderer'));
76
    }
77
}
78