Completed
Push — master ( c0e6b8...a5b3a0 )
by
unknown
05:19 queued 05:17
created

CategoryGroup::resetCraftSiteServiceSiteIdsCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.351

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 9
cp 0.5556
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 2.351
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\CategoryGroup_SiteSettings;
8
9
/**
10
 * Schematic Category Groups Converter.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class CategoryGroup extends Base
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    public function getRecordDefinition(Model $record): array
26
    {
27 2
        $definition = parent::getRecordDefinition($record);
28
29 2
        if ($record instanceof CategoryGroup_SiteSettings) {
30 2
            unset($definition['attributes']['groupId']);
31 2
            unset($definition['attributes']['siteId']);
32
        }
33
34 2
        return $definition;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function saveRecord(Model $record, array $definition): bool
41
    {
42 2
        $this->resetCraftSiteServiceSiteIdsCache();
43
44 2
        return Craft::$app->categories->saveGroup($record);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function deleteRecord(Model $record): bool
51
    {
52 2
        return Craft::$app->categories->deleteGroupById($record->id);
53
    }
54
55
    /**
56
     * Reset craft site service site ids cache using reflection.
57
     */
58 2
    private function resetCraftSiteServiceSiteIdsCache()
59
    {
60 2
        $obj = Craft::$app->sites;
61 2
        $refObject = new \ReflectionObject($obj);
62 2
        if ($refObject->hasProperty('_sitesById')) {
63
            $refProperty1 = $refObject->getProperty('_sitesById');
64
            $refProperty1->setAccessible(true);
65
            $refProperty1->setValue($obj, null);
66
            $obj->init(); // reload sites
67
        }
68 2
    }
69
}
70