Completed
Pull Request — master (#114)
by Bart
02:14
created

GlobalSets::import()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
ccs 0
cts 19
cp 0
cc 6
eloc 16
nc 10
nop 2
crap 42

1 Method

Rating   Name   Duplication   Size   Complexity  
A GlobalSets::deleteRecord() 0 4 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\elements\GlobalSet;
7
use craft\base\Model;
8
9
/**
10
 * Schematic Globals Service.
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 GlobalSets extends Base
21
{
22
    /**
23
     * Get all global sets.
24
     *
25
     * @return GlobalSet[]
26
     */
27
    protected function getRecords()
28
    {
29
        return Craft::$app->globals->getAllSets();
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function getRecordDefinition(Model $record)
36
    {
37
        $definition = parent::getRecordDefinition($record);
38
        if ($record instanceof GlobalSet) {
39
            $definition['site'] = $record->getSite()->handle;
40
            unset($definition['attributes']['tempId']);
41
            unset($definition['attributes']['uid']);
42
            unset($definition['attributes']['contentId']);
43
            unset($definition['attributes']['siteId']);
44
        }
45
46
        return $definition;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function saveRecord(Model $record, array $definition)
53
    {
54
        $site = Craft::$app->sites->getSiteByHandle($definition['site']);
55
        if ($site) {
56
            $record->siteId = $site->id;
57
        } else {
58
            Schematic::warning('Site '.$definition['site'].' could not be found');
59
        }
60
61
        return Craft::$app->globals->saveSet($record);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function deleteRecord(Model $record)
68
    {
69
        return Craft::$app->elements->deleteElementById($record->id);
70
    }
71
}
72