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

CheckUserRole::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 8
nc 4
nop 2
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