Passed
Push — master ( 430cb5...490674 )
by Craig
06:47
created

HookExtension::notifyDisplayHooks()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 27
rs 6.7272
cc 7
eloc 15
nc 6
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\HookBundle\Twig\Extension;
13
14
use Zikula\Bundle\HookBundle\Dispatcher\HookDispatcherInterface;
15
use Zikula\Bundle\HookBundle\Hook\DisplayHook;
16
use Zikula\Bundle\HookBundle\Hook\FilterHook;
17
use Zikula\Core\UrlInterface;
18
19
class HookExtension extends \Twig_Extension
20
{
21
    /**
22
     * @var HookDispatcherInterface
23
     */
24
    private $hookDispatcher;
25
26
    public function __construct(HookDispatcherInterface $hookDispatcher)
27
    {
28
        $this->hookDispatcher = $hookDispatcher;
29
    }
30
31
    /**
32
     * Returns a list of functions to add to the existing list.
33
     *
34
     * @return array An array of functions
35
     */
36
    public function getFunctions()
37
    {
38
        return [
39
            new \Twig_SimpleFunction('notifyDisplayHooks', [$this, 'notifyDisplayHooks'], ['is_safe' => ['html']])
40
        ];
41
    }
42
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter('notifyFilters', [$this, 'notifyFilters'], ['is_safe' => ['html']])
47
        ];
48
    }
49
50
    /**
51
     * @param $eventName
52
     * @param integer $id The object id
53
     * @param UrlInterface $urlObject
54
     * @param bool $outputAsArray set to true to output results as array (requires additional handling in template)
55
     *
56
     * @return bool|string|array
57
     */
58
    public function notifyDisplayHooks($eventName, $id = null, $urlObject = null, $outputAsArray = false)
59
    {
60
        if (empty($eventName)) {
61
            return trigger_error('Error! "eventname" must be set in notifydisplayhooks');
62
        }
63
        if ($urlObject && !($urlObject instanceof UrlInterface)) {
64
            return trigger_error('Error! "urlobject" must be an instance of Zikula\Core\UrlInterface');
65
        }
66
67
        // create event and notify
68
        $hook = new DisplayHook($id, $urlObject);
69
        $this->hookDispatcher->dispatch($eventName, $hook);
70
        $responses = $hook->getResponses();
71
72
        if ($outputAsArray) {
73
            return $responses;
74
        }
75
76
        $output = '';
77
        foreach ($responses as $result) {
78
            if (!empty($result)) {
79
                $output .= '<div class="z-displayhook">' . $result . '</div>' . "\n";
80
            }
81
        }
82
83
        return $output;
84
    }
85
86
    /**
87
     * @param $content
88
     * @param $filterEventName
89
     * @return mixed
90
     */
91
    public function notifyFilters($content, $filterEventName)
92
    {
93
        $hook = new FilterHook($content);
94
95
        return $this->hookDispatcher->dispatch($filterEventName, $hook)->getData();
96
    }
97
}
98