Completed
Pull Request — master (#114)
by Bart
10:52 queued 57s
created

Fields::getGroupIdByName()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 1
crap 30
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Field;
7
use craft\base\Model;
8
use craft\fields\PlainText;
9
use craft\models\FieldGroup;
10
11
/**
12
 * Schematic Fields Service.
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 Fields extends Base
23
{
24
    /**
25
     * @var number[]
26
     */
27
    private $groups;
28
29
    /**
30
     * Get all field groups
31
     *
32
     * @return FieldGroup[]
33
     */
34
    protected function getRecords()
35
    {
36
        return Craft::$app->fields->getAllFields();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function import(array $definitions, array $records = [], array $defaultAttributes = [])
43
    {
44
        parent::import($definitions, $records, $defaultAttributes);
45
        // Update field version so that contentbehavior is regenetated
46
        Craft::$app->fields->updateFieldVersion();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function getRecordDefinition(Model $record)
53
    {
54
        $definition = parent::getRecordDefinition($record);
55
        if ($record instanceof Field) {
56
            $definition['group'] = $record->group->name;
57
            unset($definition['attributes']['groupId']);
58
            unset($definition['attributes']['layoutId']);
59
            unset($definition['attributes']['tabId']);
60
        }
61
62
        return $definition;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function saveRecord(Model $record, array $definition)
69
    {
70
        $record->groupId = $this->getGroupIdByName($definition['group']);
71
        return Craft::$app->fields->saveField($record);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function deleteRecord(Model $record)
78
    {
79
        return Craft::$app->fields->deleteField($record);
80
    }
81
82
    /**
83
     * Get group id by name
84
     *
85
     * @param  string $name
86
     * @return
87
     */
88
    private function getGroupIdByName($name)
89
    {
90
        if (!isset($this->groups)) {
91
            $this->groups = [];
92
            foreach (Craft::$app->fields->getAllGroups() as $group) {
93
                $this->groups[$group->name] = $group->id;
94
            }
95
        }
96
        if (!array_key_exists($name, $this->groups)) {
97
            $group = new FieldGroup(['name' => $name]);
98
            if (Craft::$app->fields->saveGroup($group)) {
99
                $this->groups[$name] = $group->id;
100
            } else {
101
                $this->importError($group, $name);
102
            }
103
        }
104
        return $this->groups[$name];
105
    }
106
}
107