FlashMessenger::postpone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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