Passed
Pull Request — master (#166)
by
unknown
14:46 queued 04:32
created

Session   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 242
rs 9.44
wmc 37

14 Methods

Rating   Name   Duplication   Size   Complexity  
A clearFilter() 0 8 2
A setData() 0 9 2
A clearWorkspaceSort() 0 5 1
A setWorkspaceSort() 0 5 1
A toggleWorkspaceExcludeDiscardedFilter() 0 17 4
A setListAction() 0 5 1
A getListAction() 0 7 3
A getWorkspaceExcludeFilters() 0 7 3
A toggleWorkspaceBookmarksOnlyFilter() 0 17 4
A setWorkspaceItemsPerPage() 0 5 1
A getUserGlobals() 0 15 5
A getData() 0 15 4
A getWorkspaceItemsPerPage() 0 10 3
A getWorkspaceSort() 0 7 3
1
<?php
2
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
18
19
class Session
20
{
21
    const ROOT_KEY = "tx_dpf";
22
    const WORKSPACE_SORT_KEY = "workspace_sort";
23
    const LIST_ACTION_KEY = "list_action";
24
    const WORKSPACE_FILTER_KEY = "workspace_filter";
25
    const WORKSPACE_EXCLUDE_FILTER_KEY = "workspace_exclude_filter";
26
    const WORKSPACE_ITEMS_PER_PAGE = "workspace_items_per_page";
27
28
29
    /**
30
     * Stores the sort field and order.
31
     *
32
     * @param $field
33
     * @param $order
34
     */
35
    public function setWorkspaceSort($field, $order)
36
    {
37
        $sessionData = $this->getData(self::ROOT_KEY);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Helper\Session::getData() has too many arguments starting with self::ROOT_KEY. ( Ignorable by Annotation )

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

37
        /** @scrutinizer ignore-call */ 
38
        $sessionData = $this->getData(self::ROOT_KEY);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
38
        $sessionData[self::WORKSPACE_SORT_KEY] = [$field, $order];
39
        $this->setData($sessionData);
0 ignored issues
show
Bug introduced by
$sessionData of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Helper\Session::setData(). ( Ignorable by Annotation )

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

39
        $this->setData(/** @scrutinizer ignore-type */ $sessionData);
Loading history...
40
    }
41
42
    /**
43
     * Returns the stored sort field and order.
44
     *
45
     * @return array|mixed
46
     */
47
    public function getWorkspaceSort()
48
    {
49
        $sessionData = $this->getData();
50
        if (is_array($sessionData) && array_key_exists(self::WORKSPACE_SORT_KEY, $sessionData)) {
51
            return $sessionData[self::WORKSPACE_SORT_KEY];
52
        }
53
        return [];
54
    }
55
56
    /**
57
     * Deletes the sort values.
58
     */
59
    public function clearWorkspaceSort()
60
    {
61
        $sessionData = $this->getData();
62
        unset($sessionData[self::WORKSPACE_SORT_KEY]);
63
        $this->setData($sessionData);
0 ignored issues
show
Bug introduced by
$sessionData of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Helper\Session::setData(). ( Ignorable by Annotation )

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

63
        $this->setData(/** @scrutinizer ignore-type */ $sessionData);
Loading history...
64
    }
65
66
67
    /**
68
     * Deletes the filter values.
69
     */
70
    public function clearFilter()
71
    {
72
        $userGlobals = $this->getUserGlobals();
73
74
        if ($userGlobals) {
75
            $workspaceFilters = $userGlobals->getSessionData("workspaceFilters");
0 ignored issues
show
Unused Code introduced by
The assignment to $workspaceFilters is dead and can be removed.
Loading history...
76
            $workspaceFilters = [];
77
            $userGlobals->setAndSaveSessionData("workspaceFilters", $workspaceFilters);
78
        }
79
    }
80
81
82
    /**
83
     * Stores the given action name, controller name and uri.
84
     *
85
     * @param $action
86
     * @param $controller
87
     */
88
    public function setListAction($action, $controller, $uri = null)
89
    {
90
        $sessionData = $this->getData(self::ROOT_KEY);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Helper\Session::getData() has too many arguments starting with self::ROOT_KEY. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-call */ 
91
        $sessionData = $this->getData(self::ROOT_KEY);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
        $sessionData[self::LIST_ACTION_KEY] = [$action, $controller, $uri];
92
        $this->setData($sessionData);
0 ignored issues
show
Bug introduced by
$sessionData of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Helper\Session::setData(). ( Ignorable by Annotation )

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

92
        $this->setData(/** @scrutinizer ignore-type */ $sessionData);
Loading history...
93
    }
94
95
    /**
96
     * Returns the stored action name, controller name and uri.
97
     * [ 0 => 'action name', 1 => 'controller name', 2 => 'uri']
98
     *
99
     * @return array|mixed
100
     */
101
    public function getListAction()
102
    {
103
        $sessionData = $this->getData();
104
        if (is_array($sessionData) && array_key_exists(self::LIST_ACTION_KEY, $sessionData)) {
105
            return $sessionData[self::LIST_ACTION_KEY];
106
        }
107
        return [];
108
    }
109
110
111
112
    /**
113
     * Returns the exclude filters.
114
     *
115
     * @return array|mixed
116
     */
117
    public function getWorkspaceExcludeFilters()
118
    {
119
        $sessionData = $this->getData();
120
        if (is_array($sessionData) && array_key_exists(self::WORKSPACE_EXCLUDE_FILTER_KEY, $sessionData)) {
121
            return $sessionData[self::WORKSPACE_EXCLUDE_FILTER_KEY];
122
        }
123
        return [];
124
    }
125
126
    /**
127
     * Toggles the discarded documents filter.
128
     *
129
     */
130
    public function toggleWorkspaceExcludeDiscardedFilter()
131
    {
132
        $sessionData = $this->getData();
133
134
        $filters = [];
135
        if (is_array($sessionData) && array_key_exists(self::WORKSPACE_EXCLUDE_FILTER_KEY, $sessionData)) {
136
            $filters = $sessionData[self::WORKSPACE_EXCLUDE_FILTER_KEY];
137
        }
138
139
        if (array_key_exists('simpleState', $filters)) {
140
            unset($filters['simpleState']);
141
        } else {
142
            $filters['simpleState'] = [DocumentWorkflow::SIMPLE_STATE_DISCARDED];
143
        }
144
145
        $sessionData[self::WORKSPACE_EXCLUDE_FILTER_KEY] = $filters;
146
        $this->setData($sessionData);
147
    }
148
149
    /**
150
     * Toggles the hide bookmarks filter.
151
     *
152
     */
153
    public function toggleWorkspaceBookmarksOnlyFilter()
154
    {
155
        $sessionData = $this->getData();
156
157
        $filters = [];
158
        if (is_array($sessionData) && array_key_exists(self::WORKSPACE_EXCLUDE_FILTER_KEY, $sessionData)) {
159
            $filters = $sessionData[self::WORKSPACE_EXCLUDE_FILTER_KEY];
160
        }
161
162
        if (array_key_exists('bookmarks', $filters)) {
163
            unset($filters['bookmarks']);
164
        } else {
165
            $filters['bookmarks'] = true;
166
        }
167
168
        $sessionData[self::WORKSPACE_EXCLUDE_FILTER_KEY] = $filters;
169
        $this->setData($sessionData);
170
    }
171
172
173
    /**
174
     * Set the items per page for the workspace list.
175
     *
176
     * @param int $itemsPerPage
177
     */
178
    public function setWorkspaceItemsPerPage($itemsPerPage)
179
    {
180
        $sessionData = $this->getData();
181
        $sessionData[self::WORKSPACE_ITEMS_PER_PAGE] = $itemsPerPage;
182
        $this->setData($sessionData);
0 ignored issues
show
Bug introduced by
$sessionData of type array is incompatible with the type string expected by parameter $data of EWW\Dpf\Helper\Session::setData(). ( Ignorable by Annotation )

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

182
        $this->setData(/** @scrutinizer ignore-type */ $sessionData);
Loading history...
183
    }
184
185
    /**
186
     * Get the items per page for the workspace list.
187
     *
188
     * @return int
189
     */
190
    public function getWorkspaceItemsPerPage()
191
    {
192
        $sessionData = $this->getData();
193
194
        if (is_array($sessionData) && array_key_exists(self::WORKSPACE_ITEMS_PER_PAGE, $sessionData)) {
195
            $itemsPerPage = $sessionData[self::WORKSPACE_ITEMS_PER_PAGE];
196
            return $itemsPerPage;
197
        }
198
199
        return 0;
200
    }
201
202
203
    /**
204
     * Set session data
205
     *
206
     * @param string $data
207
     */
208
    public function setData($data)
209
    {
210
        $userGlobals = $this->getUserGlobals();
211
212
        if ($userGlobals) {
213
            $userGlobals->setAndSaveSessionData(self::ROOT_KEY, $data);
214
        }
215
216
        return;
217
    }
218
219
    /**
220
     * Get session data
221
     *
222
     * @return mixed
223
     */
224
    public function getData()
225
    {
226
        $userGlobals = $this->getUserGlobals();
227
228
        $sessionData = null;
229
230
        if ($userGlobals) {
231
            $sessionData = $userGlobals->getSessionData(self::ROOT_KEY);
232
        }
233
234
        if ($sessionData && is_array($sessionData)) {
235
            return $sessionData;
236
        }
237
238
        return [];
239
    }
240
241
    /**
242
     * Gets the global user object.
243
     *
244
     * @return mixed|null
245
     */
246
    protected function getUserGlobals()
247
    {
248
        $userGlobals = null;
249
250
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
251
252
            $userGlobals = $GLOBALS['TSFE']->fe_user;
253
254
        } else if (!empty($GLOBALS['BE_USER']) && is_object($GLOBALS['BE_USER'])) {
255
256
            $userGlobals = $GLOBALS['BE_USER'];
257
258
        }
259
260
        return $userGlobals;
261
    }
262
263
}
264