Completed
Pull Request — master (#114)
by Bart
04:26
created

UserGroup   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getRecordDefinition() 0 23 4
A saveRecord() 0 10 3
A deleteRecord() 0 4 1
A getAllMappedPermissions() 0 9 2
A getMappedPermissions() 0 12 4
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): array
25
    {
26
        $definition = parent::getRecordDefinition($record);
27
        $mappedPermissions = $this->getAllMappedPermissions();
28
29
        $groupPermissions = [];
30
        if ($record->id) {
31
            foreach (Craft::$app->userPermissions->getPermissionsByGroupId($record->id) as $permission) {
32
                if (array_key_exists($permission, $mappedPermissions)) {
33
                    $groupPermissions[] = $mappedPermissions[$permission];
34
                } else {
35
                    $groupPermissions[] = $permission;
36
                }
37
            }
38
        }
39
40
        $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...
41
        sort($permissionDefinitions);
42
43
        $definition['permissions'] = $permissionDefinitions;
44
45
        return $definition;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function saveRecord(Model $record, array $definition): bool
52
    {
53
        if (Craft::$app->userGroups->saveGroup($record) && array_key_exists('permissions', $definition)) {
54
            $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...
55
56
            return Craft::$app->userPermissions->saveGroupPermissions($record->id, $permissions);
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function deleteRecord(Model $record): bool
66
    {
67
        return Craft::$app->userGroups->deleteGroupById($record->id);
68
    }
69
70
    /**
71
     * Get a mapping of all permissions from lowercase to camelcase
72
     * savePermissions only accepts camelcase.
73
     *
74
     * @return array
75
     */
76
    private function getAllMappedPermissions(): array
77
    {
78
        $mappedPermissions = [];
79
        foreach (Craft::$app->userPermissions->getAllPermissions() as $permissions) {
80
            $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($permissions));
81
        }
82
83
        return $mappedPermissions;
84
    }
85
86
    /**
87
     * Recursive function to get mapped permissions.
88
     *
89
     * @param array $permissions
90
     *
91
     * @return array
92
     */
93
    private function getMappedPermissions(array $permissions): array
94
    {
95
        $mappedPermissions = [];
96
        foreach ($permissions as $permission => $options) {
97
            $mappedPermissions[strtolower($permission)] = $permission;
98
            if (is_array($options) && array_key_exists('nested', $options)) {
99
                $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($options['nested']));
100
            }
101
        }
102
103
        return $mappedPermissions;
104
    }
105
}
106