Completed
Push — master ( cc3347...ae7860 )
by Adrien
01:57
created

FlashMessenger::__toString()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.1374

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 0
dl 0
loc 36
ccs 14
cts 17
cp 0.8235
crap 5.1374
rs 9.0328
c 0
b 0
f 0
1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use Zend_Controller_Action_HelperBroker;
6
use Zend_View_Helper_Abstract;
7
8
/**
9
 * Noumenal PHP Library.
10
 *
11
 * PHP classes built on top of Zend Framework. (http://framework.zend.com/)
12
 *
13
 * Bug Reports: [email protected]
14
 * Questions  : https://noumenal.fogbugz.com/default.asp?noumenal
15
 *
16
 * LICENSE
17
 *
18
 * This source file is subject to the new BSD license that is bundled
19
 * with this package in the file noumenal-new-bsd-licence.txt.
20
 * It is also available through the world-wide-web at this URL:
21
 *
22
 * http://noumenal.co.uk/license/new-bsd
23
 *
24
 * If you did not receive a copy of the license and are unable to
25
 * obtain it through the world-wide-web, please send an email
26
 * to [email protected] so we can send you a copy immediately.
27
 *
28
 * ATTRIBUTION
29
 *
30
 * Beyond maintaining the Copyright Notice and Licence, attribution is
31
 * appreciated but not required. Please attribute where appropriate by
32
 * linking to:
33
 *
34
 * http://noumenal.co.uk/
35
 *
36
 * @author     Carlton Gibson <[email protected]>
37
 * @copyright  Copyright (c) 2009 Noumenal Software Ltd. (http://noumenal.co.uk/)
38
 * @license    http://noumenal.co.uk/license/new-bsd     New BSD License
39
 *
40
 * @version    $Revision: 3 $ $Date: 2009-08-13 16:02:49 +0100 (Thu, 13 Aug 2009) $ modified for specific purpose
41
 */
42
43
/**
44
 * View Helper to Display Flash Messages.
45
 *
46
 * Checks for messages from previous requests and from the current request.
47
 *
48
 * Checks for `array($key => $value)` pairs in FlashMessenger's messages array.
49
 * If such a pair is found, $key is taken as the "message level", $value as the
50
 * message. (Simple strings are provided a default level of 'warning'.)
51
 *
52
 * NOTE: MESSAGES ARE PRESUMED TO BE SAFE HTML. IF REDISPLAYING USER
53
 * INPUT, ESCAPE ALL MESSAGES PRIOR TO ADDING TO FLASHMESSENGER.
54
 */
55
class FlashMessenger extends Zend_View_Helper_Abstract
56
{
57
    /**
58
     * @var Zend_Controller_Action_Helper_FlashMessenger
59
     */
60
    private $_flashMessenger;
61
62
    /**
63
     * If the flashmessenger is postoned it will do nothing on first call.
64
     *
65
     * @var bool
66
     */
67
    private $isPostponed = false;
68
69
    /**
70
     * Initializing stuff. Provides fluid interface.
71
     */
72 12
    public function flashMessenger()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
73
    {
74 12
        return $this;
75
    }
76
77
    /**
78
     * Render the flash messages
79
     *
80
     * @return string flash messages formatted as div
81
     */
82 12
    public function __toString()
83
    {
84 12
        if ($this->isPostponed) {
85
            $this->isPostponed = false;
86
87
            return '';
88
        }
89
90 12
        $flashMessenger = $this->_getFlashMessenger();
91
92
        //get messages from previous requests
93 12
        $messages = $flashMessenger->getMessages();
94
95
        //add any messages from this request
96 12
        if ($flashMessenger->hasCurrentMessages()) {
97 1
            $messages = array_merge(
98 1
                    $messages, $flashMessenger->getCurrentMessages()
99
            );
100
            //we don't need to display them twice.
101 1
            $flashMessenger->clearCurrentMessages();
102
        }
103
104
        //initialise return string
105 12
        $output = '';
106
107
        //process messages
108 12
        foreach ($messages as $message) {
109 1
            $level = 'notice';
110 1
            if (is_array($message)) {
111
                [$level, $message] = each($message);
112
            }
113 1
            $output .= '<div class="flashmessenger ' . $level . '">' . $message . '</div>';
114
        }
115
116 12
        return $output;
117
    }
118
119
    /**
120
     * Postpone the flash messages to the next next call. The next call will return empty string.
121
     *
122
     * @param $isPostponed
123
     */
124
    public function postpone($isPostponed = true): void
125
    {
126
        $this->isPostponed = $isPostponed;
127
    }
128
129
    /**
130
     * Returns the flash messenger
131
     *
132
     * @return Zend_Controller_Action_Helper_FlashMessenger
133
     */
134 12
    protected function _getFlashMessenger()
135
    {
136 12
        if (null === $this->_flashMessenger) {
137 12
            $this->_flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Zend_Controller_Action_...elper('FlashMessenger') of type object<Zend_Controller_Action_Helper_Abstract> is incompatible with the declared type object<mQueue\View\Helpe..._Helper_FlashMessenger> of property $_flashMessenger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
        }
139
140 12
        return $this->_flashMessenger;
141
    }
142
}
143