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

Site::getGroupIdByName()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 6
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 Globals Service.
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
     * Get group id by name.
62
     *
63
     * @param string $name
64
     *
65
     * @return
66
     */
67
    public function getGroupIdByName($name)
68
    {
69
        if (!isset($this->groups)) {
70
            $this->groups = [];
71
            foreach (Craft::$app->sites->getAllGroups() as $group) {
72
                $this->groups[$group->name] = $group->id;
73
            }
74
        }
75
        if (!array_key_exists($name, $this->groups)) {
76
            $group = new SiteGroup(['name' => $name]);
77
            if (Craft::$app->sites->saveGroup($group)) {
78
                $this->groups[$name] = $group->id;
79
            } else {
80
                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...
81
            }
82
        }
83
84
        return $this->groups[$name];
85
    }
86
}
87