Completed
Push — master ( 874c42...3f0650 )
by Ryan
02:09
created

CheckUserRole   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 17 4
1
<?php namespace Anomaly\UsersModule\User\Command;
2
3
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface;
4
use Anomaly\UsersModule\User\Contract\UserInterface;
5
use Illuminate\Contracts\Auth\Guard;
6
use Illuminate\Contracts\Bus\SelfHandling;
7
8
/**
9
 * Class CheckUserRole
10
 *
11
 * @link          http://anomaly.is/streams-platform
12
 * @author        AnomalyLabs, Inc. <[email protected]>
13
 * @author        Ryan Thompson <[email protected]>
14
 * @package       Anomaly\UsersModule\User\Command
15
 */
16
class CheckUserRole implements SelfHandling
17
{
18
19
    /**
20
     * The role identifier.
21
     *
22
     * @var string
23
     */
24
    protected $identifier;
25
26
    /**
27
     * Create a new CheckUserRole instance.
28
     *
29
     * @param $identifier
30
     */
31
    public function __construct($identifier)
32
    {
33
        $this->identifier = $identifier;
34
    }
35
36
    /**
37
     * Handle the command.
38
     *
39
     * @param RoleRepositoryInterface $roles
40
     * @param Guard                   $guard
41
     * @return bool
42
     */
43
    public function handle(RoleRepositoryInterface $roles, Guard $guard)
44
    {
45
        /* @var UserInterface $user */
46
        if (!$user = $guard->user()) {
47
            return false;
48
        }
49
50
        if (is_numeric($this->identifier)) {
51
            return $user->hasRole($roles->find($this->identifier));
52
        }
53
54
        if (is_string($this->identifier)) {
55
            return $user->hasRole($roles->findBySlug($this->identifier));
56
        }
57
58
        return false;
59
    }
60
}
61