Completed
Push — master ( 4a1f57...0157bd )
by Daniel
01:55
created

SubsiteState::resetState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Subsites\State;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\Core\Resettable;
8
9
/**
10
 * SubsiteState provides static access to the current state for subsite related data during a request
11
 */
12
class SubsiteState implements Resettable
13
{
14
    use Injectable;
15
16
    /**
17
     * @var int|null
18
     */
19
    protected $subsiteId;
20
21
22
    /**
23
     * @var int|null
24
     */
25
    protected $originalSubsiteId;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $useSessions;
31
32
    /**
33
     * Get the current subsite ID
34
     *
35
     * @return int|null
36
     */
37
    public function getSubsiteId()
38
    {
39
        return $this->subsiteId;
40
    }
41
42
    /**
43
     * Set the current subsite ID, and track the first subsite ID set as the "original". This is used to check
44
     * whether the ID has been changed through a request.
45
     *
46
     * @param int $id
47
     * @return $this
48
     */
49
    public function setSubsiteId($id)
50
    {
51
        if (is_null($this->originalSubsiteId)) {
52
            $this->originalSubsiteId = (int) $id;
53
        }
54
55
        $this->subsiteId = (int) $id;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get whether to use sessions for storing the subsite ID
62
     *
63
     * @return bool
64
     */
65
    public function getUseSessions()
66
    {
67
        return $this->useSessions;
68
    }
69
70
    /**
71
     * Set whether to use sessions for storing the subsite ID
72
     *
73
     * @param bool $useSessions
74
     * @return $this
75
     */
76
    public function setUseSessions($useSessions)
77
    {
78
        $this->useSessions = $useSessions;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Get whether the subsite ID has been changed during a request, based on the original and current IDs
85
     *
86
     * @return bool
87
     */
88
    public function getSubsiteIdWasChanged()
89
    {
90
        return $this->originalSubsiteId !== $this->getSubsiteId();
91
    }
92
93
    /**
94
     * Perform a given action within the context of a new, isolated state. Modifications are temporary
95
     * and the existing state will be restored afterwards.
96
     *
97
     * @param callable $callback Callback to run. Will be passed the nested state as a parameter
98
     * @return mixed Result of callback
99
     */
100
    public function withState(callable $callback)
101
    {
102
        $newState = clone $this;
103
        try {
104
            Injector::inst()->registerService($newState);
105
            return $callback($newState);
106
        } finally {
107
            Injector::inst()->registerService($this);
108
        }
109
    }
110
111
    /**
112
     * Reset the local cache of the singleton
113
     */
114
    public static function reset()
115
    {
116
        SubsiteState::singleton()->resetState();
117
    }
118
119
    /**
120
     * Reset the local cache of this object
121
     */
122
    public function resetState()
123
    {
124
        $this->originalSubsiteId = null;
125
        $this->subsiteId = null;
126
        $this->useSessions = null;
127
    }
128
}
129