Completed
Push — master ( c18000...efec97 )
by Anton
12s
created

Table   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 15.96 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 15
loc 94
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoles() 0 4 1
A getBasicRoles() 0 4 1
A getUserRoles() 0 10 1
A init() 0 5 1
A getUserRolesIdentity() 15 15 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\Roles;
11
12
use Bluz\Proxy\Cache;
13
use Bluz\Proxy\Db;
14
15
/**
16
 * Class Table
17
 *
18
 * @package  Application\Roles
19
 *
20
 *
21
 * @method   static Row findRow($primaryKey)
22
 * @method   static Row findRowWhere($whereList)
23
 */
24
class Table extends \Bluz\Db\Table
25
{
26
    const BASIC_ADMIN = 'admin';
27
    const BASIC_GUEST = 'guest';
28
    const BASIC_MEMBER = 'member';
29
    const BASIC_SYSTEM = 'system';
30
31
    /**
32
     * Table
33
     *
34
     * @var string
35
     */
36
    protected $name = 'acl_roles';
37
38
    /**
39
     * Primary key(s)
40
     * @var array
41
     */
42
    protected $primary = array('id');
43
44
    /**
45
     * @var array
46
     */
47
    protected $basicRoles = ['admin', 'guest', 'member', 'system'];
48
49
    /**
50
     * Init table relations
51
     * @return void
52
     */
53 1
    public function init()
54
    {
55 1
        $this->linkTo('id', 'UsersRoles', 'roleId');
56 1
        $this->linkToMany('Users', 'UsersRoles');
57 1
    }
58
59
    /**
60
     * Get all roles in system
61
     *
62
     * @return array
63
     */
64 2
    public function getRoles()
65
    {
66 2
        return self::fetch("SELECT * FROM acl_roles ORDER BY id");
67
    }
68
69
    /**
70
     * Get all basic roles
71
     *
72
     * @return array
73
     */
74 1
    public function getBasicRoles()
75
    {
76 1
        return $this->basicRoles;
77
    }
78
79
    /**
80
     * Get all user roles in system
81
     *
82
     * @param integer $userId
83
     * @return array of rows
84
     */
85
    public function getUserRoles($userId)
86
    {
87
        $data = self::fetch(
88
            "SELECT r.*
89
            FROM acl_roles AS r, acl_users_roles AS u2r
90
            WHERE r.id = u2r.roleId AND u2r.userId = ?",
91
            array($userId)
92
        );
93
        return $data;
94
    }
95
96
    /**
97
     * Get all user roles in system
98
     *
99
     * @param integer $userId
100
     * @return array of identity
101
     */
102 4 View Code Duplication
    public function getUserRolesIdentity($userId)
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...
103
    {
104 4
        $cacheKey = 'users.roles.'.$userId;
105 4
        if (!$data = Cache::get($cacheKey)) {
106 4
            $data = Db::fetchColumn(
107
                "SELECT r.id
108
                FROM acl_roles AS r, acl_users_roles AS u2r
109
                WHERE r.id = u2r.roleId AND u2r.userId = ?
110 4
                ORDER BY r.id ASC",
111 4
                array($userId)
112
            );
113 4
            Cache::set($cacheKey, $data, Cache::TTL_NO_EXPIRY, ['system', 'users', 'roles']);
114
        }
115 4
        return $data;
116
    }
117
}
118