FlashPlugin   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 127
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A clear() 0 7 2
A getAllCurrent() 0 4 1
A getAllNext() 0 4 1
A getCurrent() 0 4 1
A getNext() 0 4 1
A keep() 0 4 1
A set() 0 7 2
A onStart() 0 6 1
A cycle() 0 9 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Session;
22
23
/**
24
 * Plugin for flash messages.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class FlashPlugin extends Plugin
30
{
31
    /**
32
     * @var \Caridea\Session\Map A session value namespace for current vals
33
     */
34
    protected $curr;
35
    /**
36
     * @var \Caridea\Session\Map A session value namespace for next vals
37
     */
38
    protected $next;
39
    /**
40
     * @var bool whether the values have been moved
41
     */
42
    protected $moved = false;
43
44
    /**
45
     * Creates a new FlashPlugin.
46
     */
47 1
    public function __construct()
48
    {
49 1
        $this->curr = new NullMap();
50 1
        $this->next = new NullMap();
51 1
    }
52
53
    /**
54
     * Removes any flash values for the _next_ request, optionally also in the _current_.
55
     *
56
     * @param bool $current If true, also clears flash values from the _current_ request.
57
     */
58 1
    public function clear(bool $current = false): void
59
    {
60 1
        if ($current) {
61 1
            $this->curr->clear();
62
        }
63 1
        $this->next->clear();
64 1
    }
65
66
    /**
67
     * Gets the flash values for the _current_ request.
68
     *
69
     * @return \Iterator The current request flash values
70
     */
71 1
    public function getAllCurrent(): \Iterator
72
    {
73 1
        return $this->curr->getIterator();
74
    }
75
76
    /**
77
     * Gets the flash values for the _next_ request.
78
     *
79
     * @return \Iterator The next request flash values
80
     */
81 1
    public function getAllNext(): \Iterator
82
    {
83 1
        return $this->next->getIterator();
84
    }
85
86
    /**
87
     * Gets the flash value for a key in the _current_ request.
88
     *
89
     * @param string $name The name
90
     * @param mixed $alt Optional default value to return
91
     * @return mixed The value, or the `$alt` if not found
92
     */
93 2
    public function getCurrent(string $name, $alt = null)
94
    {
95 2
        return $this->curr->get($name, $alt);
96
    }
97
    /**
98
     *
99
     * Gets the flash value for a key in the *next* request.
100
     *
101
     * @param string $name The name
102
     * @param mixed $alt Optional default value to return
103
     * @return mixed The value, or the `$alt` if not found
104
     */
105 2
    public function getNext(string $name, $alt = null)
106
    {
107 2
        return $this->next->get($name, $alt);
108
    }
109
110
    /**
111
     * Repeats any values in the current flash step in the next step.
112
     */
113 1
    public function keep(): void
114
    {
115 1
        $this->next->merge($this->curr);
116 1
    }
117
118
    /**
119
     * Sets a flash value for the _next_ request, optionally also in the _current_ request.
120
     *
121
     * @param string $name The name
122
     * @param mixed $value The value
123
     * @param bool $current If true, also sets flash value for the _current_ request.
124
     */
125 2
    public function set(string $name, $value, bool $current = false): void
126
    {
127 2
        if ($current) {
128 2
            $this->curr->offsetSet($name, $value);
129
        }
130 2
        $this->next->offsetSet($name, $value);
131 2
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 1
    public function onStart(Session $session): void
137
    {
138 1
        $this->curr = $session->getValues(__CLASS__ . '\\CURR');
139 1
        $this->next = $session->getValues(__CLASS__ . '\\NEXT');
140 1
        $this->cycle();
141 1
    }
142
143
    /**
144
     * Transitions the values from next to current.
145
     */
146 1
    protected function cycle()
147
    {
148 1
        if (!$this->moved) {
149 1
            $this->curr->clear();
150 1
            $this->curr->merge($this->next);
151 1
            $this->next->clear();
152 1
            $this->moved = true;
153
        }
154 1
    }
155
}
156