UserRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 114
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findByEmail() 0 4 1
A findByUsername() 0 4 1
A touchLastLogin() 0 6 1
B findByCredentials() 0 16 5
A findByResetCode() 0 4 1
A findByActivationCode() 0 4 1
A touchLastActivity() 0 7 1
1
<?php namespace Anomaly\UsersModule\User;
2
3
use Anomaly\Streams\Platform\Entry\EntryRepository;
4
use Anomaly\UsersModule\User\Contract\UserInterface;
5
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface;
6
7
/**
8
 * Class UserRepository
9
 *
10
 * @link          http://pyrocms.com/
11
 * @author        PyroCMS, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 */
14
class UserRepository extends EntryRepository implements UserRepositoryInterface
15
{
16
17
    /**
18
     * The user model.
19
     *
20
     * @var UserModel
21
     */
22
    protected $model;
23
24
    /**
25
     * Create a new UserRepository instance.
26
     *
27
     * @param UserModel $model
28
     */
29
    function __construct(UserModel $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...
30
    {
31
        $this->model = $model;
32
    }
33
34
    /**
35
     * Find a user by their credentials.
36
     *
37
     * @param  array              $credentials
38
     * @return null|UserInterface
39
     */
40
    public function findByCredentials(array $credentials)
41
    {
42
        if (isset($credentials['email'])) {
43
            $user = $this->findByEmail($credentials['email']);
44
        } elseif (isset($credentials['username'])) {
45
            $user = $this->findByUsername($credentials['username']);
46
        } else {
47
            return null;
48
        }
49
50
        if ($user && app('hash')->check($credentials['password'], $user->password)) {
0 ignored issues
show
Bug introduced by
Accessing password on the interface Anomaly\UsersModule\User\Contract\UserInterface 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...
51
            return $user;
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * Find a user by their email.
59
     *
60
     * @param $email
61
     * @return null|UserInterface
62
     */
63
    public function findByEmail($email)
64
    {
65
        return $this->model->where('email', $email)->first();
66
    }
67
68
    /**
69
     * Find a user by their username.
70
     *
71
     * @param $username
72
     * @return null|UserInterface
73
     */
74
    public function findByUsername($username)
75
    {
76
        return $this->model->where('username', $username)->first();
77
    }
78
79
    /**
80
     * Find a user by their reset code.
81
     *
82
     * @param $code
83
     * @return null|UserInterface
84
     */
85
    public function findByResetCode($code)
86
    {
87
        return $this->model->where('reset_code', $code)->first();
88
    }
89
90
    /**
91
     * Find a user by their activation code.
92
     *
93
     * @param $code
94
     * @return null|UserInterface
95
     */
96
    public function findByActivationCode($code)
97
    {
98
        return $this->model->where('activation_code', $code)->first();
99
    }
100
101
    /**
102
     * Touch a user's last activity and IP.
103
     *
104
     * @param  UserInterface $user
105
     * @return bool
106
     */
107
    public function touchLastActivity(UserInterface $user)
108
    {
109
        $user->last_activity_at = time();
0 ignored issues
show
Bug introduced by
Accessing last_activity_at on the interface Anomaly\UsersModule\User\Contract\UserInterface 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...
110
        $user->ip_address       = $_SERVER['REMOTE_ADDR'];
0 ignored issues
show
Bug introduced by
Accessing ip_address on the interface Anomaly\UsersModule\User\Contract\UserInterface 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...
111
112
        //return $this->save($user);
113
    }
114
115
    /**
116
     * Touch a user's last login.
117
     *
118
     * @param  UserInterface $user
119
     * @return bool
120
     */
121
    public function touchLastLogin(UserInterface $user)
122
    {
123
        $user->last_login_at = time();
0 ignored issues
show
Bug introduced by
Accessing last_login_at on the interface Anomaly\UsersModule\User\Contract\UserInterface 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...
124
125
        return $this->save($user);
126
    }
127
}
128