Completed
Pull Request — master (#114)
by Bart
06:18
created

UserGroups::deleteRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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
    protected $recordClass = UserGroup::class;
23
24
    /** @var string[] */
25
    private $mappedPermissions = [];
26
27
    //==============================================================================================================
28
    //================================================  EXPORT  ====================================================
29
    //==============================================================================================================
30
31
    /**
32
     * Get all section records
33
     *
34
     * @return UserGroup[]
35 4
     */
36
    protected function getRecords()
37 4
    {
38
        $this->mappedPermissions = $this->getAllMappedPermissions();
39 4
40
        return Craft::$app->userGroups->getAllGroups();
41 4
    }
42
43 4
    /**
44 3
     * Get group definition.
45 4
     *
46
     * @param UserGroupModel $group
0 ignored issues
show
Bug introduced by
There is no parameter named $group. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47 4
     *
48
     * @return array
49
     */
50
    protected function getRecordDefinition(Model $record)
51
    {
52
        $attributes = parent::getRecordDefinition($record);
53
        $attributes['permissions'] = $this->getGroupPermissionDefinitions($record);
54
        return $attributes;
55
    }
56
57 3
    /**
58
     * Get group permissions.
59
     *
60 3
     * @param $group
61 3
     *
62 3
     * @return array|string
63
     */
64
    private function getGroupPermissionDefinitions($group)
65
    {
66
        $permissionDefinitions = [];
67
        $groupPermissions = Craft::$app->userPermissions->getPermissionsByGroupId($group->id);
68
69
        foreach ($groupPermissions as $permission) {
70
            if (array_key_exists($permission, $this->mappedPermissions)) {
71
                $permission = $this->mappedPermissions[$permission];
72 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...
73
            }
74 3
        }
75 3
        sort($permissionDefinitions);
76
77 3
        return $permissionDefinitions;
78 1
    }
79 1
80 1
    /**
81 1
     * Get a mapping of all permissions from lowercase to camelcase
82 3
     * savePermissions only accepts camelcase.
83 3
     *
84
     * @return array
85 3
     */
86
    private function getAllMappedPermissions()
87
    {
88
        $mappedPermissions = [];
89
        foreach (Craft::$app->userPermissions->getAllPermissions() as $permissions) {
90
            $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($permissions));
91
        }
92
93
        return $mappedPermissions;
94 4
    }
95
96 4
    /**
97 4
     * @param array $permissions
98 4
     *
99 4
     * @return array
100
     */
101 4
    private function getMappedPermissions(array $permissions)
102
    {
103
        $mappedPermissions = [];
104
        foreach ($permissions as $permission => $options) {
105
            $mappedPermissions[strtolower($permission)] = $permission;
106
            if (array_key_exists('nested', $options)) {
107
                $mappedPermissions = array_merge($mappedPermissions, $this->getMappedPermissions($options['nested']));
108
            }
109 4
        }
110
111 4
        return $mappedPermissions;
112 4
    }
113 4
114 4
    //==============================================================================================================
115 4
    //================================================  IMPORT  ====================================================
116 4
    //==============================================================================================================
117 4
118
    /**
119 4
     * Save a record
120
     *
121
     * @param Model $record
122
     * @return boolean
123
     */
124
    protected function saveRecord(Model $record)
125
    {
126
        return Craft::$app->userGroups->saveGroup($record);
127
    }
128
129
    /**
130
     * Delete a record
131
     *
132
     * @param Model $record
133
     * @return boolean
134 9
     */
135
    protected function deleteRecord(Model $record)
136 9
    {
137
        return Craft::$app->userGroups->deleteGroup($record);
138 9
    }
139
}
140