Completed
Push — master ( b99c1e...eaeef0 )
by Ryan
02:07
created

UserRepository::touchLastActivity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 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
 * @package       Anomaly\UsersModule\User
14
 */
15
class UserRepository extends EntryRepository implements UserRepositoryInterface
16
{
17
18
    /**
19
     * The user model.
20
     *
21
     * @var UserModel
22
     */
23
    protected $model;
24
25
    /**
26
     * Create a new UserRepository instance.
27
     *
28
     * @param UserModel $model
29
     */
30
    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...
31
    {
32
        $this->model = $model;
33
    }
34
35
    /**
36
     * Find a user by their credentials.
37
     *
38
     * @param array $credentials
39
     * @return null|UserInterface
40
     */
41
    public function findByCredentials(array $credentials)
42
    {
43
        if (isset($credentials['email'])) {
44
            $user = $this->findByEmail($credentials['email']);
45
        } elseif (isset($credentials['username'])) {
46
            $user = $this->findByUsername($credentials['username']);
47
        } else {
48
            return null;
49
        }
50
51
        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...
52
            return $user;
53
        }
54
55
        return null;
56
    }
57
58
    /**
59
     * Find a user by their email.
60
     *
61
     * @param $email
62
     * @return null|UserInterface
63
     */
64
    public function findByEmail($email)
65
    {
66
        return $this->model->where('email', $email)->first();
67
    }
68
69
    /**
70
     * Find a user by their username.
71
     *
72
     * @param $username
73
     * @return null|UserInterface
74
     */
75
    public function findByUsername($username)
76
    {
77
        return $this->model->where('username', $username)->first();
78
    }
79
80
    /**
81
     * Find a user by their reset code.
82
     *
83
     * @param $code
84
     * @return null|UserInterface
85
     */
86
    public function findByResetCode($code)
87
    {
88
        return $this->model->where('reset_code', $code)->first();
89
    }
90
91
    /**
92
     * Find a user by their activation code.
93
     *
94
     * @param $code
95
     * @return null|UserInterface
96
     */
97
    public function findByActivationCode($code)
98
    {
99
        return $this->model->where('activation_code', $code)->first();
100
    }
101
102
    /**
103
     * Touch a user's last activity and IP.
104
     *
105
     * @param UserInterface $user
106
     * @return bool
107
     */
108
    public function touchLastActivity(UserInterface $user)
109
    {
110
        $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...
111
        $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...
112
113
        return $this->save($user);
114
    }
115
116
    /**
117
     * Touch a user's last login.
118
     *
119
     * @param UserInterface $user
120
     * @return bool
121
     */
122
    public function touchLastLogin(UserInterface $user)
123
    {
124
        $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...
125
126
        return $this->save($user);
127
    }
128
}
129