Test Setup Failed
Pull Request — master (#4522)
by Craig
08:26 queued 03:47
created

HookRuntime::createRouteUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\HookBundle\Twig\Runtime;
15
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Twig\Extension\RuntimeExtensionInterface;
18
use Zikula\Bundle\CoreBundle\RouteUrl;
19
use Zikula\Bundle\CoreBundle\UrlInterface;
20
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface;
21
use Zikula\Bundle\HookBundle\Hook\DisplayHook;
22
use Zikula\Bundle\HookBundle\Hook\FilterHook;
23
24
class HookRuntime implements RuntimeExtensionInterface
25
{
26
    /**
27
     * @var RequestStack
28
     */
29
    private $requestStack;
30
31
    /**
32
     * @var HookDispatcherInterface
33
     */
34
    private $hookDispatcher;
35
36
    public function __construct(
37
        RequestStack $requestStack,
38
        HookDispatcherInterface $hookDispatcher
39
    ) {
40
        $this->requestStack = $requestStack;
41
        $this->hookDispatcher = $hookDispatcher;
42
    }
43
44
    /**
45
     * @return bool|string|array
46
     */
47
    public function notifyDisplayHooks(string $eventName, int $id = null, UrlInterface $urlObject = null, bool $outputAsArray = false)
48
    {
49
        if (empty($eventName)) {
50
            return trigger_error('Error! "eventname" must be set in notifydisplayhooks');
51
        }
52
        if ($urlObject && !($urlObject instanceof UrlInterface)) {
0 ignored issues
show
introduced by
$urlObject is always a sub-type of Zikula\Bundle\CoreBundle\UrlInterface.
Loading history...
53
            return trigger_error('Error! "urlobject" must be an instance of Zikula\Bundle\CoreBundle\UrlInterface');
54
        }
55
56
        // create event and notify
57
        $hook = new DisplayHook($id, $urlObject);
58
        $this->hookDispatcher->dispatch($eventName, $hook);
59
        $responses = $hook->getResponses();
60
61
        if ($outputAsArray) {
62
            return $responses;
63
        }
64
65
        $output = '';
66
        foreach ($responses as $result) {
67
            if (null === $result) {
68
                continue;
69
            }
70
            $output .= '<div class="z-displayhook">' . $result . '</div>' . "\n";
71
        }
72
73
        return $output;
74
    }
75
76
    public function createRouteUrl(string $routeName, array $routeParameters = [], string $fragment = null): UrlInterface
77
    {
78
        $url = new RouteUrl($routeName, $routeParameters, $fragment);
79
80
        if (!isset($routeParameters['_locale']) && null !== $this->requestStack->getCurrentRequest()) {
81
            $url->setLanguage($this->requestStack->getCurrentRequest()->getLocale());
82
        }
83
84
        return $url;
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90
    public function notifyFilters(string $content, string $filterEventName)
91
    {
92
        $hook = $this->hookDispatcher->dispatch($filterEventName, new FilterHook($content));
93
        if ($hook instanceof FilterHook) {
94
            return $hook->getData();
95
        }
96
97
        return $content;
98
    }
99
}
100