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

Field   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecordDefinition() 0 16 2
A saveRecord() 0 8 2
A deleteRecord() 0 4 1
B getGroupIdByName() 0 19 5
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Base;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\FieldGroup;
8
use NerdsAndCompany\Schematic\Schematic;
9
use NerdsAndCompany\Schematic\Converters\Models\Base;
10
11
/**
12
 * Schematic Fields 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 Field extends Base
23
{
24
    /**
25
     * @var number[]
26
     */
27
    private $groups;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getRecordDefinition(Model $record)
33
    {
34
        $definition = parent::getRecordDefinition($record);
35
36
        if ($record->groupId) {
37
            $definition['group'] = $record->group->name;
38
        }
39
40
        $definition['attributes']['required'] = $definition['attributes']['required'] == true;
41
        unset($definition['attributes']['context']);
42
        unset($definition['attributes']['groupId']);
43
        unset($definition['attributes']['layoutId']);
44
        unset($definition['attributes']['tabId']);
45
46
        return $definition;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function saveRecord(Model $record, array $definition)
53
    {
54
        if (array_key_exists('group', $definition)) {
55
            $record->groupId = $this->getGroupIdByName($definition['group']);
56
        }
57
58
        return Craft::$app->fields->saveField($record);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function deleteRecord(Model $record)
65
    {
66
        return Craft::$app->fields->deleteField($record);
67
    }
68
69
    /**
70
     * Get group id by name.
71
     *
72
     * @param string $name
73
     *
74
     * @return
75
     */
76
    private function getGroupIdByName($name)
77
    {
78
        if (!isset($this->groups)) {
79
            $this->groups = [];
80
            foreach (Craft::$app->fields->getAllGroups() as $group) {
81
                $this->groups[$group->name] = $group->id;
82
            }
83
        }
84
        if (!array_key_exists($name, $this->groups)) {
85
            $group = new FieldGroup(['name' => $name]);
86
            if (Craft::$app->fields->saveGroup($group)) {
87
                $this->groups[$name] = $group->id;
88
            } else {
89
                return Schematic::importError($group, $name);
90
            }
91
        }
92
93
        return $this->groups[$name];
94
    }
95
}
96