RolesTableTrait::buildPermissionRelationship()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * CakePHP permission handling library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\CakePermission\Model\Table;
7
8
use Cake\Validation\Validator;
9
use Slince\CakePermission\Constants;
10
use Cake\Cache\Cache;
11
use Cake\Core\Configure;
12
use Slince\CakePermission\TableFactory;
13
14
trait RolesTableTrait
15
{
16
    public function buildPermissionRelationship()
17
    {
18
        $this->belongsToMany('Permissions', [
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
            'className' => TableFactory::getModelClass('Permissions'),
20
            'foreignKey' => 'role_id',
21
            'targetForeignKey' => 'permission_id',
22
            'joinTable' => Configure::read('Permission.tableNameMap.roles_permissions') ?: 'roles_permissions',
23
            'saveStrategy' => 'append'
24
        ]);
25
        $this->belongsToMany('Users', [
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
            'className' => TableFactory::getModelClass('Users'),
27
            'foreignKey' => 'role_id',
28
            'targetForeignKey' => 'user_id',
29
            'joinTable' => Configure::read('Permission.tableNameMap.users_roles') ?: 'users_roles',
30
            'saveStrategy' => 'append'
31
        ]);
32
        $this->addBehavior('Timestamp');
0 ignored issues
show
Bug introduced by
It seems like addBehavior() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
    }
34
35
    /**
36
     * @param Validator $validator
37
     * @return Validator
38
     */
39
    public function validationPermission(Validator $validator)
40
    {
41
        $validator->add('name', 'unique', [
42
            'rule' => 'validateUnique',
43
            'message' => 'The role already exists',
44
            'provider' => 'table'
45
        ]);
46
        return $validator;
47
    }
48
49
    /**
50
     * Refreshes the cache
51
     * @param int $userId
52
     */
53
    public static function refreshCache($userId)
54
    {
55
        Cache::delete(sprintf(Constants::CACHE_ROLES, $userId));
56
    }
57
}