Passed
Push — fix-9163 ( 64dde5...3df94b )
by Sam
17:47 queued 10:54
created

GridFieldStateManager::getStateFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace SilverStripe\Forms\GridField;
5
6
use SilverStripe\Control\HTTP;
7
use SilverStripe\Control\HTTPRequest;
8
9
/**
10
 * Creates a unique key for the gridfield, and uses that to write to and retrieve
11
 * its state from the request
12
 */
13
class GridFieldStateManager implements GridFieldStateManagerInterface
14
{
15
    /**
16
     * @param GridField $gridField
17
     * @return string
18
     */
19
    public function getStateKey(GridField $gridField): string
20
    {
21
        $i = 0;
22
        $form = $gridField->getForm();
23
        if ($form) {
0 ignored issues
show
introduced by
$form is of type SilverStripe\Forms\Form, thus it always evaluated to true.
Loading history...
24
            $controller = $form->getController();
25
            while ($controller instanceof GridFieldDetailForm_ItemRequest) {
26
                $controller = $controller->getController();
27
                $i++;
28
            }
29
        }
30
31
        return sprintf('%s-%s-%s', 'gridState', $gridField->getName(), $i);
32
    }
33
34
    /**
35
     * @param GridField $gridField
36
     * @param string $url
37
     * @return string
38
     */
39
    public function addStateToURL(GridField $gridField, string $url): string
40
    {
41
        $key = $this->getStateKey($gridField);
42
        $value = $gridField->getState(false)->Value();
43
44
        return HTTP::setGetVar($key, $value, $url);
45
    }
46
47
    /**
48
     * @param GridField $gridField
49
     * @param HTTPRequest $request
50
     * @return string|null
51
     */
52
    public function getStateFromRequest(GridField $gridField, HTTPRequest $request): ?string
53
    {
54
        return $request->requestVar($this->getStateKey($gridField));
55
    }
56
}
57