SingleSwitcher::restoreBlog()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace MultisiteGlobalMedia;
5
6
/**
7
 * Class SingleSwitcher
8
 */
9
class SingleSwitcher implements SiteSwitcher
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $switched = false;
15
16
    /**
17
     * Switch to blog if needed
18
     *
19
     * @param int $siteId
20
     */
21
    public function switchToBlog(int $siteId)
22
    {
23
        if (get_current_blog_id() === $siteId) {
24
            return;
25
        }
26
27
        switch_to_blog($siteId);
28
29
        $this->switched = true;
30
    }
31
32
    /**
33
     * Restore the current blog if needed
34
     */
35
    public function restoreBlog()
36
    {
37
        if (!$this->switched) {
38
            return;
39
        }
40
41
        restore_current_blog();
42
43
        $this->switched = false;
44
    }
45
}
46