Completed
Push — master ( ae47f3...33ec04 )
by Bob Olde
11s
created

GlobalSet::saveRecord()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Elements;
4
5
use Craft;
6
use craft\elements\GlobalSet as GlobalSetElement;
7
use craft\base\Model;
8
use NerdsAndCompany\Schematic\Schematic;
9
use NerdsAndCompany\Schematic\Converters\Models\Base;
10
11
/**
12
 * Schematic Globals Converter.
13
 *
14
 * Sync Craft Setups.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class GlobalSet extends Base
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getRecordDefinition(Model $record): array
28
    {
29
        $definition = parent::getRecordDefinition($record);
30
31
        if ($record instanceof GlobalSetElement) {
32
            $definition['site'] = $record->getSite()->handle;
33
            unset($definition['attributes']['tempId']);
34
            unset($definition['attributes']['uid']);
35
            unset($definition['attributes']['contentId']);
36
            unset($definition['attributes']['siteId']);
37
            unset($definition['attributes']['hasDescendants']);
38
            unset($definition['attributes']['ref']);
39
            unset($definition['attributes']['status']);
40
            unset($definition['attributes']['totalDescendants']);
41
            unset($definition['attributes']['url']);
42
43
            foreach ($record->getFieldLayout()->getFields() as $field) {
44
                unset($definition['attributes'][$field->handle]);
45
            }
46
        }
47
48
        return $definition;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function saveRecord(Model $record, array $definition): bool
55
    {
56
        if (array_key_exists('site', $definition)) {
57
            $site = Craft::$app->sites->getSiteByHandle($definition['site']);
58
            if ($site) {
59
                $record->siteId = $site->id;
60
            } else {
61
                Schematic::error('Site '.$definition['site'].' could not be found');
62
63
                return false;
64
            }
65
        }
66
67
        return Craft::$app->globals->saveSet($record);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function deleteRecord(Model $record): bool
74
    {
75
        return Craft::$app->elements->deleteElementById($record->id);
76
    }
77
}
78