Completed
Pull Request — master (#248)
by Anton
05:45
created

Table   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 94
ccs 9
cts 21
cp 0.4286
rs 10
wmc 5
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrivileges() 0 8 1
B getUserPrivileges() 0 33 2
A getRolePrivileges() 18 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Privileges;
11
12
use Application\Roles;
13
use Bluz\Proxy\Cache;
14
use Bluz\Proxy\Db;
15
16
/**
17
 * Table
18
 *
19
 * @package  Application\Privileges
20
 *
21
 * @method   static Row findRow($primaryKey)
22
 * @method   static Row findRowWhere($whereList)
23
 */
24
class Table extends \Bluz\Db\Table
25
{
26
    /**
27
     * Table
28
     *
29
     * @var string
30
     */
31
    protected $table = 'acl_privileges';
32
33
    /**
34
     * Primary key(s)
35
     * @var array
36
     */
37
    protected $primary = array('roleId', 'module', 'privilege');
38
39
    /**
40
     * Get all privileges
41
     *
42
     * @return array
43
     */
44 1
    public function getPrivileges()
45
    {
46 1
        return $this->fetch(
47
            "SELECT DISTINCT p.roleId, p.module, p.privilege
48
            FROM acl_privileges AS p
49
            ORDER BY module, privilege"
50 1
        );
51
    }
52
53
    /**
54
     * Get user privileges
55
     *
56
     * @param integer $userId
57
     * @return array
58
     */
59 3
    public function getUserPrivileges($userId)
60
    {
61 3
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($userId);
62
63 3
        $stack = [];
64 3
        foreach ($roles as $roleId) {
65
            $stack = array_merge($stack, $this->getRolePrivileges($roleId));
66 3
        }
67
68
        // magic array_unique for multi array
69 3
        return array_unique($stack);
70
71
        // follow code is faster, but required record for every user in memcache
72
        // in other words, need more memory for decrease CPU load
73
        // for update
74
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        $cacheKey = 'privileges:user:'.$userId;
76
        if (!$data = Cache::get($cacheKey)) {
77
            $data = Db::getDefaultAdapter()->fetchColumn(
78
                "SELECT DISTINCT r.id, CONCAT(p.module, ':', p.privilege)
79
                FROM acl_privileges AS p, acl_roles AS r, acl_users_roles AS u2r
80
                WHERE p.roleId = r.id AND r.id = u2r.roleId AND u2r.userId = ?
81
                ORDER BY module, privilege",
82
                array((int) $userId)
83
            );
84
85
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY);
86
            Cache::addTag($cacheKey, 'privileges');
87
            Cache::addTag($cacheKey, 'user:'.$userId);
88
        }
89
        return $data;
90
        */
91
    }
92
93
    /**
94
     * Get user privileges
95
     *
96
     * @param integer $roleId
97
     * @return array
98
     */
99 View Code Duplication
    public function getRolePrivileges($roleId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $cacheKey = 'privileges:role:'.$roleId;
102
103
        if (!$data = Cache::get($cacheKey)) {
104
            $data = Db::fetchColumn(
105
                "SELECT DISTINCT CONCAT(p.module, ':', p.privilege)
106
                FROM acl_privileges AS p, acl_roles AS r
107
                WHERE p.roleId = r.id AND r.id = ?
108
                ORDER BY module, privilege",
109
                array((int) $roleId)
110
            );
111
112
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY);
113
            Cache::addTag($cacheKey, 'privileges');
114
        }
115
        return $data;
116
    }
117
}
118