Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

FlashMessages::addWarningMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service;
11
12
use Slick\Filter\StaticFilter;
13
use Slick\Http\SessionDriverInterface;
14
15
/**
16
 * FlashMessages
17
 *
18
 * @package Slick\Mvc\Service
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
class FlashMessages
22
{
23
    /**
24
     * @var SessionDriverInterface
25
     */
26
    private $sessionDriver;
27
28
    /**#@+
29
     * @const string TYPE for message type constants
30
     */
31
    const TYPE_SUCCESS = 0;
32
    const TYPE_ERROR   = 1;
33
    const TYPE_WARNING = 2;
34
    const TYPE_INFO    = 3;
35
    /**#@-*/
36
37
    /**
38
     * @var array
39
     */
40
    private static $messages = [];
41
42
    /**
43
     * Creates a flash messages service
44
     *
45
     * @param SessionDriverInterface $sessionDriver
46
     */
47
    public function __construct(SessionDriverInterface $sessionDriver)
48
    {
49
        $this->sessionDriver = $sessionDriver;
50
    }
51
52
    /**
53
     * Set a flash message of a give type
54
     *
55
     * @param int    $type
56
     * @param string $message
57
     *
58
     * @return FlashMessages
59
     */
60
    public function set($type, $message)
61
    {
62
        $type = StaticFilter::filter('number', $type);
63
        if ($type === '' || $type < 0 || $type > 3) {
64
            $type = self::TYPE_INFO;
65
        }
66
        self::$messages[$type][] = $message;
67
        $this->sessionDriver->set('_messages_', self::$messages);
68
        return $this;
69
    }
70
71
    /**
72
     * clears all messages
73
     *
74
     * @return FlashMessages
75
     */
76
    public function flush()
77
    {
78
        self::$messages = [];
79
        $this->sessionDriver->erase('_messages_');
80
        return $this;
81
    }
82
83
    /**
84
     * Retrieve all messages and flushes them all
85
     *
86
     * @return array
87
     */
88
    public function get()
89
    {
90
        $messages = $this->sessionDriver->get('_messages_', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $this->flush();
92
        return $messages;
93
    }
94
95
    /**
96
     * Add an info flash message
97
     *
98
     * @param string $message
99
     * @return self
100
     */
101
    public function addInfoMessage($message)
102
    {
103
        return $this->set(FlashMessages::TYPE_INFO, $message);
104
    }
105
    /**
106
     * Add a warning flash message
107
     *
108
     * @param string $message
109
     * @return self
110
     */
111
    public function addWarningMessage($message)
112
    {
113
        return $this->set(FlashMessages::TYPE_WARNING, $message);
114
    }
115
    /**
116
     * Add an error flash message
117
     *
118
     * @param string $message
119
     * @return self
120
     */
121
    public function addErrorMessage($message)
122
    {
123
        return $this->set(FlashMessages::TYPE_ERROR, $message);
124
    }
125
    /**
126
     * Add a success flash message
127
     *
128
     * @param string $message
129
     * @return self
130
     */
131
    public function addSuccessMessage($message)
132
    {
133
        return $this->set(FlashMessages::TYPE_SUCCESS, $message);
134
    }
135
}