Completed
Pull Request — master (#114)
by Bart
18:18 queued 08:14
created

Field::getRecordDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
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\Converters\Models\Base;
9
10
/**
11
 * Schematic Fields Converter.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class Field extends Base
22
{
23
    /**
24
     * @var number[]
25
     */
26
    private $groups;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getRecordDefinition(Model $record)
32
    {
33
        $definition = parent::getRecordDefinition($record);
34
35
        $definition['group'] = $record->group->name;
36
        unset($definition['attributes']['groupId']);
37
        unset($definition['attributes']['layoutId']);
38
        unset($definition['attributes']['tabId']);
39
40
        return $definition;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function saveRecord(Model $record, array $definition)
47
    {
48
        $record->groupId = $this->getGroupIdByName($definition['group']);
49
50
        return Craft::$app->fields->saveField($record);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function deleteRecord(Model $record)
57
    {
58
        return Craft::$app->fields->deleteField($record);
59
    }
60
61
    /**
62
     * Get group id by name.
63
     *
64
     * @param string $name
65
     *
66
     * @return
67
     */
68
    private function getGroupIdByName($name)
69
    {
70
        if (!isset($this->groups)) {
71
            $this->groups = [];
72
            foreach (Craft::$app->fields->getAllGroups() as $group) {
73
                $this->groups[$group->name] = $group->id;
74
            }
75
        }
76
        if (!array_key_exists($name, $this->groups)) {
77
            $group = new FieldGroup(['name' => $name]);
78
            if (Craft::$app->fields->saveGroup($group)) {
79
                $this->groups[$name] = $group->id;
80
            } else {
81
                $this->importError($group, $name);
0 ignored issues
show
Documentation Bug introduced by
The method importError does not exist on object<NerdsAndCompany\S...\Converters\Base\Field>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
82
            }
83
        }
84
85
        return $this->groups[$name];
86
    }
87
}
88