GlobalSet   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 56
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 23 3
A saveRecord() 0 15 3
A deleteRecord() 0 4 1
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-2019, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class GlobalSet extends Base
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function getRecordDefinition(Model $record): array
28
    {
29 2
        $definition = parent::getRecordDefinition($record);
30
31 2
        if ($record instanceof GlobalSetElement) {
32 2
            $definition['site'] = $record->getSite()->handle;
33 2
            unset($definition['attributes']['tempId']);
34 2
            unset($definition['attributes']['uid']);
35 2
            unset($definition['attributes']['contentId']);
36 2
            unset($definition['attributes']['siteId']);
37 2
            unset($definition['attributes']['hasDescendants']);
38 2
            unset($definition['attributes']['ref']);
39 2
            unset($definition['attributes']['status']);
40 2
            unset($definition['attributes']['totalDescendants']);
41 2
            unset($definition['attributes']['url']);
42
43 2
            foreach ($record->getFieldLayout()->getFields() as $field) {
44 2
                unset($definition['attributes'][$field->handle]);
45
            }
46
        }
47
48 2
        return $definition;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 2
    public function saveRecord(Model $record, array $definition): bool
55
    {
56 2
        if (array_key_exists('site', $definition)) {
57 2
            $site = Craft::$app->sites->getSiteByHandle($definition['site']);
58 2
            if ($site) {
59 1
                $record->siteId = $site->id;
60
            } else {
61 1
                Schematic::error('Site '.$definition['site'].' could not be found');
62
63 1
                return false;
64
            }
65
        }
66
67 1
        return Craft::$app->globals->saveSet($record);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function deleteRecord(Model $record): bool
74
    {
75 2
        return Craft::$app->elements->deleteElementById($record->id);
76
    }
77
}
78