RoleRepository   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 134
Duplicated Lines 16.42 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 91.38%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 18
c 3
b 0
f 2
lcom 2
cbo 4
dl 22
loc 134
ccs 53
cts 58
cp 0.9138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAll() 0 4 1
A getById() 0 4 1
A getBySlug() 0 4 1
B create() 5 28 5
C update() 17 38 7
A delete() 0 10 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
/**
4
 * @package     Dashboard
5
 * @author      Ian Olson <[email protected]>
6
 * @license     MIT
7
 * @copyright   2015, Laraflock
8
 * @link        https://github.com/laraflock
9
 */
10
11
namespace Laraflock\Dashboard\Repositories\Role;
12
13
use Cartalyst\Sentinel\Roles\EloquentRole;
14
use Cartalyst\Sentinel\Sentinel;
15
use Illuminate\Database\QueryException;
16
use Laraflock\Dashboard\Exceptions\RolesException;
17
use Laraflock\Dashboard\Repositories\Base\BaseRepository;
18
19
class RoleRepository extends BaseRepository implements RoleRepositoryInterface
20
{
21
    /**
22
     * EloquentRole instance.
23
     *
24
     * @var \Cartalyst\Sentinel\Roles\EloquentRole
25
     */
26
    protected $role;
27
28
    /**
29
     * Sentinel instance.
30
     *
31
     * @var \Cartalyst\Sentinel\Sentinel
32
     */
33
    protected $sentinel;
34
35 110
    public function __construct(EloquentRole $role, Sentinel $sentinel)
36
    {
37 110
        $this->role     = $role;
38 110
        $this->sentinel = $sentinel;
39 110
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 4
    public function getAll()
45
    {
46 4
        return $this->role->all();
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 14
    public function getById($id)
53
    {
54 14
        return $this->sentinel->findRoleById($id);
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    public function getBySlug($slug)
61
    {
62 1
        return $this->sentinel->findRoleBySlug($slug);
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 97
    public function create(array $data, $validate = true)
69
    {
70 97
        $this->rules = [
71 97
          'slug' => 'required|alpha_dash|unique:roles',
72 97
          'name' => 'required|alpha_dash|unique:roles',
73
        ];
74
75 97
        if ($validate) {
76 97
            $this->validate($data);
77 97
        }
78
79
        // Convert the checkbox values of "1" to true, so permission checking works with Sentinel.
80 97 View Code Duplication
        if (isset($data['permissions'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81 3
            foreach ($data['permissions'] as $permission => $value) {
82 3
                $data['permissions'][$permission] = true;
83 3
            }
84 3
        }
85
86
        try {
87 97
            $role = $this->sentinel->getRoleRepository()
88 97
                                   ->createModel()
89 97
                                   ->create($data);
90 97
        } catch (QueryException $e) {
91 1
            throw new RolesException(trans('dashboard::dashboard.errors.role.create'));
92
        }
93
94 97
        return $role;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 6
    public function update(array $data, $id, $validate = true)
101
    {
102 6
        if (!$role = $this->getById($id)) {
103 2
            throw new RolesException(trans('dashboard::dashboard.errors.role.found'));
104
        }
105
106 4 View Code Duplication
        if ($role->name != $data['name']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107 4
            $this->rules['name'] = 'required|alpha_dash|unique:roles';
108 4
        } else {
109
            $this->rules['name'] = 'required|alpha_dash';
110
        }
111
112 4 View Code Duplication
        if ($role->slug != $data['slug']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113 2
            $this->rules['slug'] = 'required|alpha_dash|unique:roles';
114 2
        } else {
115 2
            $this->rules['slug'] = 'required|alpha_dash';
116
        }
117
118 4
        if ($validate) {
119 4
            $this->validate($data);
120 2
        }
121
122
        // Convert the checkbox values of "1" to true, so permission checking works with Sentinel.
123 2 View Code Duplication
        if (isset($data['permissions'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
124
            foreach ($data['permissions'] as $permission => $value) {
125
                $data['permissions'][$permission] = true;
126
            }
127
        } else {
128 2
            $data['permissions'] = [];
129
        }
130
131 2
        $role->name        = $data['name'];
132 2
        $role->slug        = $data['slug'];
133 2
        $role->permissions = $data['permissions'];
134 2
        $role->save();
135
136 2
        return $role;
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 4
    public function delete($id)
143
    {
144 4
        if (!$role = $this->getById($id)) {
145 2
            throw new RolesException(trans('dashboard::dashboard.errors.role.found'));
146
        }
147
148 2
        $role->delete();
149
150 2
        return true;
151
    }
152
}