Completed
Pull Request — master (#114)
by Bart
01:44
created

UserGroup::getMappedPermissions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\base\Model;
7
8
/**
9
 * Schematic User Groups Service.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class UserGroup extends Base
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getRecordDefinition(Model $record)
25
    {
26
        $attributes = parent::getRecordDefinition($record);
27
        $mappedPermissions = $this->getAllMappedPermissions();
28
29
        $groupPermissions = [];
30
        foreach (Craft::$app->userPermissions->getPermissionsByGroupId($record->id) as $permission) {
31
            if (array_key_exists($permission, $mappedPermissions)) {
32
                $groupPermissions[] = $mappedPermissions[$permission];
33
            } else {
34
                $groupPermissions[] = $permission;
35
            }
36
        }
37
38
        $permissionDefinitions = $this->getSources(false, $groupPermissions, 'id', 'handle');
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\S...rters\Models\UserGroup>? 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...
39
        sort($permissionDefinitions);
40
41
        $attributes['permissions'] = $permissionDefinitions;
42
43
        return $attributes;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function saveRecord(Model $record, array $definition)
50
    {
51
        if (Craft::$app->userGroups->saveGroup($record)) {
52
            $permissions = $this->getSources(false, $definition['permissions'], 'handle', 'id');
0 ignored issues
show
Documentation Bug introduced by
The method getSources does not exist on object<NerdsAndCompany\S...rters\Models\UserGroup>? 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...
53
54
            return Craft::$app->userPermissions->saveGroupPermissions($record->id, $permissions);
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function deleteRecord(Model $record)
64
    {
65
        return Craft::$app->userGroups->deleteGroup($record);
66
    }
67
68
    /**
69
     * Get a mapping of all permissions from lowercase to camelcase
70
     * savePermissions only accepts camelcase.
71
     *
72
     * @return array
73
     */
74
    private function getAllMappedPermissions()
75
    {
76
        $mappedPermissions = [];
77
        foreach (Craft::$app->userPermissions->getAllPermissions() as $permissions) {
78
            $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($permissions));
79
        }
80
81
        return $mappedPermissions;
82
    }
83
84
    /**
85
     * Recursive function to get mapped permissions.
86
     *
87
     * @param array $permissions
88
     *
89
     * @return array
90
     */
91
    private function getMappedPermissions(array $permissions)
92
    {
93
        $mappedPermissions = [];
94
        foreach ($permissions as $permission => $options) {
95
            $mappedPermissions[strtolower($permission)] = $permission;
96
            if (is_array($options) && array_key_exists('nested', $options)) {
97
                $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($options['nested']));
98
            }
99
        }
100
101
        return $mappedPermissions;
102
    }
103
}
104