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

Site::getRecordDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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