Completed
Push — master ( 5cf2d8...46bcff )
by Damian
13s
created

SubsiteState::setSubsiteId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\Subsites\State;
4
5
use SilverStripe\Core\Injector\Injectable;
6
use SilverStripe\Core\Injector\Injector;
7
8
/**
9
 * SubsiteState provides static access to the current state for subsite related data during a request
10
 */
11
class SubsiteState
12
{
13
    use Injectable;
14
15
    /**
16
     * @var int|null
17
     */
18
    protected $subsiteId;
19
20
    /**
21
     * Get the current subsite ID
22
     *
23
     * @return int|null
24
     */
25
    public function getSubsiteId()
26
    {
27
        return $this->subsiteId;
28
    }
29
30
    /**
31
     * Set the current subsite ID
32
     *
33
     * @param int $id
34
     * @return $this
35
     */
36
    public function setSubsiteId($id)
37
    {
38
        $this->subsiteId = (int) $id;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Perform a given action within the context of a new, isolated state. Modifications are temporary
45
     * and the existing state will be restored afterwards.
46
     *
47
     * @param callable $callback Callback to run. Will be passed the nested state as a parameter
48
     * @return mixed Result of callback
49
     */
50
    public function withState(callable $callback)
51
    {
52
        $newState = clone $this;
53
        try {
54
            Injector::inst()->registerService($newState);
55
            return $callback($newState);
56
        } finally {
57
            Injector::inst()->registerService($this);
58
        }
59
    }
60
}
61