Completed
Push — master ( 03fc4c...95d208 )
by Ralf
15s queued 12s
created

Session::setStoredAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace EWW\Dpf\Session;
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
class Session
18
{
19
    const ROOT_KEY = "tx_dpf";
20
    const LIST_ACTION_KEY = "list_action";
21
    const WORKSPACE = "workspace";
22
23
24
    /**
25
     * @return SearchSessionData $data
26
     */
27
    public function getWorkspaceData()
28
    {
29
        $sessionData = $this->getData(self::ROOT_KEY);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Session\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

29
        /** @scrutinizer ignore-call */ 
30
        $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...
30
        if (array_key_exists(self::WORKSPACE, $sessionData)) {
31
            return unserialize($sessionData[self::WORKSPACE]);
32
        }
33
34
        return new SearchSessionData();
35
    }
36
37
    /**
38
     * @param SearchSessionData $data
39
     */
40
    public function setWorkspaceData(SearchSessionData $data)
41
    {
42
        $sessionData[self::WORKSPACE] = serialize($data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$sessionData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sessionData = array(); before regardless.
Loading history...
43
        $this->setData($sessionData);
44
    }
45
46
47
    /**
48
     * Stores the given action name, controller name and uri.
49
     *
50
     * @param $action
51
     * @param $controller
52
     */
53
    public function setStoredAction($action, $controller, $uri = null)
54
    {
55
        $sessionData = $this->getData(self::ROOT_KEY);
0 ignored issues
show
Unused Code introduced by
The call to EWW\Dpf\Session\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

55
        /** @scrutinizer ignore-call */ 
56
        $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...
56
        $sessionData[self::LIST_ACTION_KEY] = [$action, $controller, $uri];
57
        $this->setData($sessionData);
58
    }
59
60
    /**
61
     * Returns the stored action name, controller name and uri.
62
     * [ 0 => 'action name', 1 => 'controller name', 2 => 'uri']
63
     *
64
     * @return array|mixed
65
     */
66
    public function getStoredAction()
67
    {
68
        $sessionData = $this->getData();
69
        if (is_array($sessionData) && array_key_exists(self::LIST_ACTION_KEY, $sessionData)) {
70
            return $sessionData[self::LIST_ACTION_KEY];
71
        }
72
        return [];
73
    }
74
75
76
    /**
77
     * Set session data
78
     *
79
     * @param array $data
80
     */
81
    public function setData($data)
82
    {
83
        $userGlobals = $this->getUserGlobals();
84
85
        if ($userGlobals) {
86
            $userGlobals->setAndSaveSessionData(self::ROOT_KEY, $data);
87
        }
88
89
        return;
90
    }
91
92
    /**
93
     * Get session data
94
     *
95
     * @return array
96
     */
97
    public function getData()
98
    {
99
        $userGlobals = $this->getUserGlobals();
100
101
        $sessionData = null;
102
103
        if ($userGlobals) {
104
            $sessionData = $userGlobals->getSessionData(self::ROOT_KEY);
105
        }
106
107
        if ($sessionData && is_array($sessionData)) {
108
            return $sessionData;
109
        }
110
111
        return [];
112
    }
113
114
    /**
115
     * Gets the global user object.
116
     *
117
     * @return mixed|null
118
     */
119
    protected function getUserGlobals()
120
    {
121
        $userGlobals = null;
122
123
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
124
125
            $userGlobals = $GLOBALS['TSFE']->fe_user;
126
127
        } else if (!empty($GLOBALS['BE_USER']) && is_object($GLOBALS['BE_USER'])) {
128
129
            $userGlobals = $GLOBALS['BE_USER'];
130
131
        }
132
133
        return $userGlobals;
134
    }
135
136
}
137