Completed
Pull Request — master (#114)
by Bart
09:33 queued 07:46
created

UserGroups::import()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
ccs 20
cts 20
cp 1
rs 8.439
cc 6
eloc 17
nc 10
nop 2
crap 6
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\base\Model;
7
use craft\models\UserGroup;
8
9
/**
10
 * Schematic User Groups 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 UserGroups extends Base
21
{
22
    /** @var string[] */
23
    private $mappedPermissions = [];
24
25
    /**
26
     * Get all section records
27
     *
28
     * @return UserGroup[]
29
     */
30
    protected function getRecords()
31
    {
32
        $this->mappedPermissions = $this->getAllMappedPermissions();
33
34
        return Craft::$app->userGroups->getAllGroups();
35 4
    }
36
37 4
    /**
38
     * {@inheritdoc}
39 4
     */
40
    protected function getRecordDefinition(Model $record)
41 4
    {
42
        $attributes = parent::getRecordDefinition($record);
43 4
        $attributes['permissions'] = $this->getGroupPermissionDefinitions($record);
44 3
        return $attributes;
45 4
    }
46
47 4
    /**
48
     * Get group permissions.
49
     *
50
     * @param $group
51
     *
52
     * @return array|string
53
     */
54
    private function getGroupPermissionDefinitions($group)
55
    {
56
        $permissionDefinitions = [];
57 3
        $groupPermissions = Craft::$app->userPermissions->getPermissionsByGroupId($group->id);
58
59
        foreach ($groupPermissions as $permission) {
60 3
            if (array_key_exists($permission, $this->mappedPermissions)) {
61 3
                $permission = $this->mappedPermissions[$permission];
62 3
                $permissionDefinitions[] = $this->getSource(false, $permission, 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSource does not exist on object<NerdsAndCompany\S...ic\Services\UserGroups>? 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...
63
            }
64
        }
65
        sort($permissionDefinitions);
66
67
        return $permissionDefinitions;
68
    }
69
70
    /**
71
     * Get a mapping of all permissions from lowercase to camelcase
72 3
     * savePermissions only accepts camelcase.
73
     *
74 3
     * @return array
75 3
     */
76
    private function getAllMappedPermissions()
77 3
    {
78 1
        $mappedPermissions = [];
79 1
        foreach (Craft::$app->userPermissions->getAllPermissions() as $permissions) {
80 1
            $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($permissions));
81 1
        }
82 3
83 3
        return $mappedPermissions;
84
    }
85 3
86
    /**
87
     * @param array $permissions
88
     *
89
     * @return array
90
     */
91
    private function getMappedPermissions(array $permissions)
92
    {
93
        $mappedPermissions = [];
94 4
        foreach ($permissions as $permission => $options) {
95
            $mappedPermissions[strtolower($permission)] = $permission;
96 4
            if (array_key_exists('nested', $options)) {
97 4
                $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($options['nested']));
98 4
            }
99 4
        }
100
101 4
        return $mappedPermissions;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function saveRecord(Model $record, array $definition)
108
    {
109 4
        Schematic::warning('Importing usergroup permissions is not yet implemented');
110
        return Craft::$app->userGroups->saveGroup($record);
111 4
    }
112 4
113 4
    /**
114 4
     * {@inheritdoc}
115 4
     */
116 4
    protected function deleteRecord(Model $record)
117 4
    {
118
        return Craft::$app->userGroups->deleteGroup($record);
119 4
    }
120
}
121