RoleRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 74
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findBySlug() 0 4 1
A findByPermission() 0 10 2
A updatePermissions() 0 8 1
A allButAdmin() 0 4 1
1
<?php namespace Anomaly\UsersModule\Role;
2
3
use Anomaly\Streams\Platform\Entry\EntryCollection;
4
use Anomaly\Streams\Platform\Entry\EntryRepository;
5
use Anomaly\UsersModule\Role\Contract\RoleInterface;
6
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class RoleRepositoryInterface
11
 *
12
 * @link          http://pyrocms.com/
13
 * @author        PyroCMS, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 */
16
class RoleRepository extends EntryRepository implements RoleRepositoryInterface
17
{
18
19
    /**
20
     * The role model.
21
     *
22
     * @var RoleModel
23
     */
24
    protected $model;
25
26
    /**
27
     * Create a new RoleRepositoryInterface instance.
28
     *
29
     * @param RoleModel $model
30
     */
31
    function __construct(RoleModel $model)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
    {
33
        $this->model = $model;
34
    }
35
36
    /**
37
     * Return all but the admin role.
38
     *
39
     * @return RoleCollection
40
     */
41
    public function allButAdmin()
42
    {
43
        return $this->model->where('slug', '!=', 'admin')->get();
44
    }
45
46
    /**
47
     * Find a role by it's slug.
48
     *
49
     * @param $slug
50
     * @return null|RoleInterface
51
     */
52
    public function findBySlug($slug)
53
    {
54
        return $this->model->where('slug', $slug)->first();
55
    }
56
57
    /**
58
     * Find a role by a permission key.
59
     *
60
     * @param $permission
61
     * @return null|EntryCollection
62
     */
63
    public function findByPermission($permission)
64
    {
65
        $query = $this->model->newQuery();
66
67
        foreach ((array)$permission as $key) {
68
            $query->where('permissions', 'LIKE', '%"' . str_replace('*', '%', $key) . '"%');
69
        }
70
71
        return $query->get();
72
    }
73
74
    /**
75
     * Update permissions for a role.
76
     *
77
     * @param  RoleInterface $role
78
     * @param  array         $permissions
79
     * @return RoleInterface
80
     */
81
    public function updatePermissions(RoleInterface $role, array $permissions)
82
    {
83
        $role->permissions = $permissions;
0 ignored issues
show
Bug introduced by
Accessing permissions on the interface Anomaly\UsersModule\Role\Contract\RoleInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
84
85
        $role->save();
86
87
        return $role;
88
    }
89
}
90