SingleSwitcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A switchToBlog() 0 9 2
A restoreBlog() 0 9 2
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