Completed
Push — master ( ca1336...ce2897 )
by Adrien
07:43
created

FlashMessenger::postpone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
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
 * @version    $Revision: 3 $ $Date: 2009-08-13 16:02:49 +0100 (Thu, 13 Aug 2009) $ modified for specific purpose
40
 */
41
42
/**
43
 * View Helper to Display Flash Messages.
44
 *
45
 * Checks for messages from previous requests and from the current request.
46
 *
47
 * Checks for `array($key => $value)` pairs in FlashMessenger's messages array.
48
 * If such a pair is found, $key is taken as the "message level", $value as the
49
 * message. (Simple strings are provided a default level of 'warning'.)
50
 *
51
 * NOTE: MESSAGES ARE PRESUMED TO BE SAFE HTML. IF REDISPLAYING USER
52
 * INPUT, ESCAPE ALL MESSAGES PRIOR TO ADDING TO FLASHMESSENGER.
53
 */
54
class FlashMessenger extends Zend_View_Helper_Abstract
0 ignored issues
show
Coding Style introduced by
The property $_flashMessenger is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
55
{
56
    /**
57
     * @var Zend_Controller_Action_Helper_FlashMessenger
58
     */
59
    private $_flashMessenger = null;
60
61
    /**
62
     * If the flashmessenger is postoned it will do nothing on first call.
63
     * @var bool
64
     */
65
    private $isPostponed = false;
66
67
    /**
68
     * Initializing stuff. Provides fluid interface.
69
     */
70 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...
71
    {
72 12
        return $this;
73
    }
74
75
    /**
76
     * Render the flash messages
77
     * @return string flash messages formatted as div
78
     */
79 12
    public function __toString()
80
    {
81 12
        if ($this->isPostponed) {
82
            $this->isPostponed = false;
83
84
            return '';
85
        }
86
87 12
        $flashMessenger = $this->_getFlashMessenger();
88
89
        //get messages from previous requests
90 12
        $messages = $flashMessenger->getMessages();
91
92
        //add any messages from this request
93 12
        if ($flashMessenger->hasCurrentMessages()) {
94 1
            $messages = array_merge(
95 1
                    $messages, $flashMessenger->getCurrentMessages()
96
            );
97
            //we don't need to display them twice.
98 1
            $flashMessenger->clearCurrentMessages();
99
        }
100
101
        //initialise return string
102 12
        $output = '';
103
104
        //process messages
105 12
        foreach ($messages as $message) {
106 1
            $level = 'notice';
107 1
            if (is_array($message)) {
108
                list($level, $message) = each($message);
109
            }
110 1
            $output .= '<div class="flashmessenger ' . $level . '">' . $message . '</div>';
111
        }
112
113 12
        return $output;
114
    }
115
116
    /**
117
     * Postpone the flash messages to the next next call. The next call will return empty string.
118
     * @param $isPostponed
119
     */
120
    public function postpone($isPostponed = true)
121
    {
122
        $this->isPostponed = $isPostponed;
123
    }
124
125
    /**
126
     * Returns the flash messenger
127
     * @return Zend_Controller_Action_Helper_FlashMessenger
128
     */
129 12
    protected function _getFlashMessenger()
130
    {
131 12
        if (null === $this->_flashMessenger) {
132 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...
133
        }
134
135 12
        return $this->_flashMessenger;
136
    }
137
}
138