Completed
Push — after-import-callback ( 808d90...eae689 )
by
unknown
15:02 queued 11:10
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 9
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 1
    public function getMapperHandle(): string
25
    {
26 1
        return 'modelMapper';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    public function getRecords(): array
33
    {
34 1
        return Craft::$app->sites->getAllSites();
35
    }
36
37
    /**
38
     * Reset craft site service sites cache using reflection.
39
     */
40
    public function afterImport()
41
    {
42
        $obj = Craft::$app->sites;
43
        $refObject = new \ReflectionObject($obj);
44
        if ($refObject->hasProperty('_sitesById')) {
45
            $refProperty1 = $refObject->getProperty('_sitesById');
46
            $refProperty1->setAccessible(true);
47
            $refProperty1->setValue($obj, null);
48
        }
49
        if ($refObject->hasProperty('_sitesByHandle')) {
50
            $refProperty2 = $refObject->getProperty('_sitesByHandle');
51
            $refProperty2->setAccessible(true);
52
            $refProperty2->setValue($obj, null);
53
        }
54
        $obj->init(); // reload sites
55
    }
56
}
57