FlashMessenger::renderMessages()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 12
nop 4
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\View\Helper;
4
5
use Zend\View\Helper\FlashMessenger as ZendFlashMessenger;
6
use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger;
7
8
class FlashMessenger extends ZendFlashMessenger
0 ignored issues
show
Deprecated Code introduced by
The class Zend\View\Helper\FlashMessenger has been deprecated with message: This helper will be removed in version 3.0 of this component. At that time, it will be available in zendframework/zend-mvc-plugin-flashmessenger.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
9
{
10
    protected $classMessages = array(
11
        PluginFlashMessenger::NAMESPACE_INFO => 'alert-info',
12
        PluginFlashMessenger::NAMESPACE_ERROR => 'alert-danger',
13
        PluginFlashMessenger::NAMESPACE_SUCCESS => 'alert-success',
14
        PluginFlashMessenger::NAMESPACE_DEFAULT => 'alert-info',
15
        PluginFlashMessenger::NAMESPACE_WARNING => 'alert-warning',
16
    );
17
18
    public function renderAll()
19
    {
20
        $flashMessenger = $this->getPluginFlashMessenger();
21
        $markup = '';
22
        foreach ($this->classMessages as $namespace => $class) {
23
            $messages = $flashMessenger->getMessagesFromNamespace($namespace);
24
            $markup .= $this->renderMessages($namespace, $messages);
25
        }
26
27
        return $markup;
28
    }
29
30
    protected function renderMessages(
31
        $namespace = PluginFlashMessenger::NAMESPACE_DEFAULT,
32
        array $messages = array(),
33
        array $classes = array(),
34
        $autoEscape = null
35
    ) {
36
        // Prepare classes for opening tag
37
        if (empty($classes)) {
38
            if (isset($this->classMessages[$namespace])) {
39
                $classes = $this->classMessages[$namespace];
40
            } else {
41
                $classes = $this->classMessages[PluginFlashMessenger::NAMESPACE_DEFAULT];
42
            }
43
            $classes = array($classes);
44
        }
45
46
        if (null === $autoEscape) {
47
            $autoEscape = $this->getAutoEscape();
48
        }
49
        $messagesToPrint = $this->flattenMessages($messages, $autoEscape);
50
51
        // Generate markup
52
        $markup = '';
53
        foreach ($messagesToPrint as $message) {
54
            $markup .= $this->getView()->alert($message, ['class' => current($classes)], true);
0 ignored issues
show
Bug introduced by
The method alert() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        }
56
57
        return $markup;
58
    }
59
60
    /**
61
     * @param array $messages
62
     * @param $autoEscape
63
     * @return array
64
     */
65
    private function flattenMessages(array $messages, $autoEscape)
66
    {
67
        // Flatten message array
68
        $escapeHtml = $this->getEscapeHtmlHelper();
69
        $messagesToPrint = array();
70
        $translator = $this->getTranslator();
71
        $translatorTextDomain = $this->getTranslatorTextDomain();
72
        array_walk_recursive(
73
            $messages,
74
            function($item) use (& $messagesToPrint, $escapeHtml, $autoEscape, $translator, $translatorTextDomain) {
75
                if ($translator !== null) {
76
                    $item = $translator->translate(
77
                        $item,
78
                        $translatorTextDomain
79
                    );
80
                }
81
82
                if ($autoEscape) {
83
                    $messagesToPrint[] = $escapeHtml($item);
84
                    return;
85
                }
86
87
                $messagesToPrint[] = $item;
88
            }
89
        );
90
        return $messagesToPrint;
91
    }
92
}