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

UserGroup   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 84
rs 10
c 0
b 0
f 0

5 Methods

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