Completed
Pull Request — master (#143)
by Bart
14:28 queued 10:30
created

SiteDataType::resetCraftSiteServiceSiteIdsCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace NerdsAndCompany\Schematic\DataTypes;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Schematic;
7
8
/**
9
 * Schematic Sites DataType.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class SiteDataType extends Base
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getMapperHandle(): string
25
    {
26
        return 'modelMapper';
27 1
    }
28
29 1
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getRecords(): array
33
    {
34
        return Craft::$app->sites->getAllSites();
35
    }
36
37 1
    /**
38
     * {@inheritdoc}
39 1
     */
40
    public function afterImport()
41
    {
42
        $this->resetCraftSiteServiceSiteIdsCache();
43
    }
44
45
    /**
46
     * Reset craft site service site ids cache using reflection.
47
     */
48
    private function resetCraftSiteServiceSiteIdsCache()
49
    {
50
        $obj = Craft::$app->sites;
51
        $refObject = new \ReflectionObject($obj);
52
        if ($refObject->hasProperty('_sitesById')) {
53
            $refProperty1 = $refObject->getProperty('_sitesById');
54
            $refProperty1->setAccessible(true);
55
            $refProperty1->setValue($obj, null);
56
            $obj->init(); // reload sites
57
        }
58
    }
59
}
60