Completed
Push — master ( c32227...e62f89 )
by Simon
02:04
created

ShortListController::getSecurityToken()   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 0
1
<?php
2
use Jaybizzle\CrawlerDetect\CrawlerDetect;
3
4
class ShortListController extends Page_Controller
5
{
6
    private static $allowed_actions = array(
7
        'renderList',
8
        'performAction'
9
    );
10
11
    private static $url_handlers = array(
12
        'add'       => 'performAction',
13
        'remove'    => 'performAction',
14
        '$URL!'     => 'renderList',
15
    );
16
17
    private static $extensions = array(
18
        'ShortListPaginationExtension'
19
    );
20
21
    public function init()
22
    {
23
        parent::init();
24
25
        Session::start();
26
27
        if ($this->request->getVar('page')) {
28
            $this->currentPage = $this->request->getVar('page');
29
        }
30
    }
31
32
    /**
33
     * When landing on the homepage, if there is a shortlist for the current
34
     * user, redirect to the correct URL. Otherwise, 404.
35
     * */
36
    public function index($request)
37
    {
38
        if (($shortlist = $this->getSessionShortList())) {
39
            return $this->redirect(Config::inst()->get('ShortList', 'URLSegment').$shortlist->URL);
40
        } else {
41
            $CrawlerDetect = new CrawlerDetect;
42
43
            // Check the user agent of the current 'visitor'
44
            if ($CrawlerDetect->isCrawler()) {
45
                return $this->httpError(403);
46
            }
47
48
            $shortlist = $this->getSessionShortList();
49
50
            if (!$shortlist || !$shortlist->exists()) {
51
                $shortlist = new ShortList();
52
                $shortlist->write();
53
            }
54
        }
55
56
        // render with empty template.
57
        return $this->renderWith(array('Page', 'ShortList_empty'));
58
    }
59
60
    /**
61
     * Get the absolute URL of this controller.
62
     * */
63
    public function Link($action = null)
64
    {
65
        $shortlist = $this->getSessionShortList();
66
        $url = Config::inst()->get('ShortList', 'URLSegment');
67
68
        if ($shortlist) {
69
            $url .= $shortlist->URL;
70
        }
71
72
        return $url;
73
    }
74
75
    public function renderList($request)
76
    {
77
        $shortlist = DataObject::get_one('ShortList', $filter = array('URL' => $request->param('URL')));
78
        $link = false;
79
        $count = 0;
80
81
        if ($this->dontRender($shortlist, $request)) {
82
            return $this->httpError(404);
83
        }
84
85
        if ($shortlist && $shortlist->exists()) {
86
            $link = $shortlist->Link();
87
            $count = $shortlist->ShortListItems()->Count();
88
        }
89
90
        return $this->customise(array(
91
            'ShortlistURL' => $link,
92
            'ShortlistCount' => $count
93
        ))->renderWith(
94
            array('ShortList', 'Page')
95
        );
96
    }
97
98
    public function performAction($request)
99
    {
100
        if ($this->dontPerformAction($request)) {
101
            return $this->httpError(404);
102
        }
103
104
        $action = new AddToshortlistAction();
105
        $matches = array();
106
107
        preg_match('/remove|add/', $request->getURL(), $matches);
108
109
        if ($matches[0] == 'remove') {
110
            $action = new RemoveFromshortlistAction();
111
        }
112
113
        $status = $action->performAction(
114
            $shortlist = $this->getSessionShortList(),
1 ignored issue
show
Documentation introduced by
$shortlist = $this->getSessionShortList() is of type object<DataObject>, but the function expects a null|object<ShortList>.

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...
115
            $ID = $request->getVar('id'),
116
            $type = $request->getVar('type'),
117
            $session = $request->getVar('s')
118
        );
119
120
        if ($request->isAjax()) {
121
            return $this->renderAjax($session);
122
        }
123
124
        if (array_key_exists('output', $request->getVars())) {
125
            return $status;
126
        }
127
128
        return $this->redirectBack();
129
    }
130
131
132
    /**
133
     * Get the number of items in the current short list.
134
     *
135
     * @param session The session to check & find a shortlist for.
136
     * @return mixed false if no session exists - else the number of items in the shortlist.
137
     * */
138
    public function shortListCount($session = false)
139
    {
140
        if ($this->isSessionValid($session)) {
141
            return false;
142
        }
143
144
        $shortlist = $this->getSessionShortList();
145
146
        if (!$shortlist || !$shortlist->exists()) {
147
            return 0;
148
        }
149
150
        return $shortlist->Items()->count();
151
    }
152
153
    public static function getShortListSession()
154
    {
155
        return DataObject::get_one('ShortList', $filter = array('SessionID' => self::getSecurityToken()));
156
    }
157
158
    /**
159
     * Get the token to use to add/remove from shortlist.
160
     * */
161
    public static function getSecurityToken()
162
    {
163
        return Utilities::getSecurityToken();
164
    }
165
166
    /**
167
     * Return a valid shortlist - or null.
168
     * */
169
    private function getSessionShortList()
170
    {
171
        return DataObject::get_one('ShortList',
172
            $filter = array('SessionID' => self::getSecurityToken()),
173
            $cache = false
174
        );
175
    }
176
177
    /**
178
     * Return the json encoded count & url for the current session
179
     * */
180
    private function renderAjax($session) {
181
        $shortlist = $this->getSessionShortList();
182
        $url = false;
183
184
        if ($shortlist && $shortlist->exists()) {
185
            $url = $shortlist->Link();
186
        }
187
188
        return json_encode(array(
189
            'count' => $this->shortListCount($session),
190
            'url' => $url
191
        ));
192
    }
193
194
    /**
195
     * Don't render the template!
196
     * */
197
    private function dontRender($shortlist, $request)
198
    {
199
        return is_null(self::getSecurityToken()) || !$request->param('URL') || !$shortlist || !$shortlist->exists();
200
    }
201
202
    /**
203
     * Is this session valid?
204
     * */
205
    private function isSessionValid($session) {
206
        return is_null(self::getSecurityToken()) || !$session || $session != self::getSecurityToken();
207
    }
208
209
    /**
210
     * Don't perform an action.
211
     * */
212
    private function dontPerformAction($request)
213
    {
214
        return is_null(self::getSecurityToken()) || !$request->getVar('id') || !$request->getVar('type') ||
215
            !$request->getVar('s') || $request->getVar('s') != self::getSecurityToken();
216
    }
217
}
218