Completed
Push — master ( 3f0650...a210b6 )
by Ryan
02:07
created

GetUser::handle()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 5
nop 2
1
<?php namespace Anomaly\UsersModule\User\Command;
2
3
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface;
4
use Illuminate\Contracts\Auth\Guard;
5
use Illuminate\Contracts\Bus\SelfHandling;
6
7
/**
8
 * Class GetUser
9
 *
10
 * @link          http://anomaly.is/streams-platform
11
 * @author        AnomalyLabs, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 * @package       Anomaly\UsersModule\User\Command
14
 */
15
class GetUser implements SelfHandling
16
{
17
18
    /**
19
     * The user identifier.
20
     *
21
     * @var mixed
22
     */
23
    protected $identifier;
24
25
    /**
26
     * Create a new GetUser instance.
27
     *
28
     * @param $identifier
29
     */
30
    public function __construct($identifier)
31
    {
32
        $this->identifier = $identifier;
33
    }
34
35
    /**
36
     * Handle the command.
37
     *
38
     * @param UserRepositoryInterface $users
39
     * @param Guard                   $auth
40
     * @return \Anomaly\UsersModule\User\Contract\UserInterface|\Illuminate\Contracts\Auth\Authenticatable|null
41
     */
42
    public function handle(UserRepositoryInterface $users, Guard $auth)
43
    {
44
        if (is_null($this->identifier)) {
45
            return $auth->user();
46
        }
47
48
        if (is_numeric($this->identifier)) {
49
            return $users->find($this->identifier);
50
        }
51
52
        if (filter_var($this->identifier, FILTER_VALIDATE_EMAIL)) {
53
            return $users->findByEmail($this->identifier);
54
        }
55
56
        if (!is_numeric($this->identifier)) {
57
            return $users->findByUsername($this->identifier);
58
        }
59
60
        return null;
61
    }
62
}
63