SessionToolbarController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license.
20
 */
21
22
namespace SanSessionToolbar\Controller;
23
24
use Laminas\Http\PhpEnvironment\Request;
25
use Laminas\Mvc\Controller\AbstractActionController;
26
use Laminas\Validator\NotEmpty;
27
use Laminas\View\Model\JsonModel;
28
use Laminas\View\Renderer\RendererInterface;
29
use SanSessionToolbar\Manager\SessionManagerInterface;
30
31
/**
32
 * Session Toolbar Controller.
33
 *
34
 * @author Abdul Malik Ikhsan <[email protected]>
35
 */
36
final class SessionToolbarController extends AbstractActionController
37
{
38
    /**
39
     * Construct.
40
     */
41
    public function __construct(private readonly RendererInterface $viewRenderer, private readonly SessionManagerInterface $sessionManager)
42
    {
43
    }
44
45
    /**
46
     * Remove Session by Container and its key.
47
     */
48
    public function removesessionAction()
49
    {
50
        $success = false;
51
        $request = $this->getEvent()->getRequest();
52
        if ($request instanceof Request && $request->isPost()) {
53
            $containerName = $request->getPost('containerName', 'Default');
54 13
            $keysession = $request->getPost('keysession', '');
55
56 13
            $success = $this->sessionManager
57 13
                            ->sessionSetting($containerName, $keysession);
0 ignored issues
show
Bug introduced by
It seems like $containerName can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $containerName of SanSessionToolbar\Manage...rface::sessionSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                            ->sessionSetting(/** @scrutinizer ignore-type */ $containerName, $keysession);
Loading history...
Bug introduced by
It seems like $keysession can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $keysession of SanSessionToolbar\Manage...rface::sessionSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                            ->sessionSetting($containerName, /** @scrutinizer ignore-type */ $keysession);
Loading history...
58 13
        }
59
60
        return new JsonModel([
61
            'success' => $success,
62
        ]);
63 3
    }
64
65 3
    /**
66 3
     * Reload Session data.
67 3
     */
68 2
    public function reloadsessionAction()
69 2
    {
70
        $sessionData = $this->sessionManager->getSessionData(false);
71 2
72 2
        $renderedContent = $this->viewRenderer
73
                                ->render('laminas-developer-tools/toolbar/session-data-list', ['sessionData' => $sessionData]);
74
75 3
        return new JsonModel([
76 3
            'san_sessiontoolbar_data_renderedContent' => $renderedContent,
77
        ]);
78
    }
79
80
    /**
81
     * Clear Session data.
82
     */
83 2
    public function clearsessionAction()
84
    {
85 2
        $request = $this->getEvent()->getRequest();
86
        if ($request instanceof Request && $request->isPost()) {
87 2
            $this->sessionManager->clearSession($request->getPost('byContainer'));
0 ignored issues
show
Bug introduced by
It seems like $request->getPost('byContainer') can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $byContainer of SanSessionToolbar\Manage...terface::clearSession() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            $this->sessionManager->clearSession(/** @scrutinizer ignore-type */ $request->getPost('byContainer'));
Loading history...
88 2
        }
89
90 2
        $sessionData = $this->sessionManager->getSessionData();
91 2
        $renderedContent = $this->viewRenderer
92
                                ->render('laminas-developer-tools/toolbar/session-data-list', ['sessionData' => $sessionData]);
93
94
        return new JsonModel([
95
            'san_sessiontoolbar_data_renderedContent' => $renderedContent,
96
        ]);
97
    }
98 2
99
    /**
100 2
     * Save Session by Container and its key.
101 2
     */
102 2
    public function savesessionAction()
103
    {
104
        $processSetOrAddSessionData = ['success' => false, 'errorMessage' => ''];
105 2
        $request = $this->getEvent()->getRequest();
106 2
107 2
        if ($request instanceof Request && $request->isPost()) {
108
            $processSetOrAddSessionData = $this->setOrAddSession($request);
109 2
        }
110 2
111
        $sessionData = $this->sessionManager->getSessionData();
112
        $renderedContent = $this->viewRenderer
113
                                ->render('laminas-developer-tools/toolbar/session-data-list', ['sessionData' => $sessionData]);
114
115
        return new JsonModel([
116
            'success' => $processSetOrAddSessionData['success'],
117 5
            'errorMessage' => $processSetOrAddSessionData['errorMessage'],
118
            'san_sessiontoolbar_data_renderedContent' => $renderedContent,
119 5
        ]);
120 5
    }
121
122 5
    /**
123 5
     * Set or Add Session Data Process.
124
     *
125
     *
126 5
     * @return array
127 5
     */
128 5
    private function setOrAddSession(Request $request)
129
    {
130 5
        $containerName = $request->getPost('containerName', 'Default');
131 5
        $keysession = $request->getPost('keysession', '');
132 5
        $sessionValue = $request->getPost('sessionvalue', '');
133 5
        $new = (bool) $request->getPost('new', false);
134
135
        $notEmpty = new NotEmpty();
136
        if ($notEmpty->isValid($keysession) && $notEmpty->isValid($sessionValue)) {
137
            $success = $this->sessionManager
138
                            ->sessionSetting($containerName, $keysession, $sessionValue, ['set' => true, 'new' => $new]);
0 ignored issues
show
Bug introduced by
It seems like $sessionValue can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $value of SanSessionToolbar\Manage...rface::sessionSetting() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
                            ->sessionSetting($containerName, $keysession, /** @scrutinizer ignore-type */ $sessionValue, ['set' => true, 'new' => $new]);
Loading history...
Bug introduced by
It seems like $containerName can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $containerName of SanSessionToolbar\Manage...rface::sessionSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
                            ->sessionSetting(/** @scrutinizer ignore-type */ $containerName, $keysession, $sessionValue, ['set' => true, 'new' => $new]);
Loading history...
Bug introduced by
It seems like $keysession can also be of type Laminas\Stdlib\ParametersInterface; however, parameter $keysession of SanSessionToolbar\Manage...rface::sessionSetting() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
                            ->sessionSetting($containerName, /** @scrutinizer ignore-type */ $keysession, $sessionValue, ['set' => true, 'new' => $new]);
Loading history...
139
140
            return ['success' => $success, 'errorMessage' => ''];
141
        }
142
143
        return ['success' => false, 'errorMessage' => "Value is required and can't be empty"];
144 5
    }
145
}
146