UserRepository::resetPassword()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
3
4
5
/**
6
 * Class UserRepository
7
 *
8
 * This service abstracts some interactions that occurs between Confide and
9
 * the Database.
10
 */
11
class UserRepository
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /**
14
     * Signup a new account with the given parameters
15
     *
16
     * @param  array $input Array containing 'email' and 'password'.
17
     *
18
     * @return  User User object that may or may not be saved successfully. Check the id to make sure.
19
     */
20
    public function signup($input)
21
    {
22
        $user = new User;
23
24
        $user->email    = array_get($input, 'email');
25
        $user->password = array_get($input, 'password');
26
27
        // The password confirmation will be removed from model
28
        // before saving. This field will be used in Ardent's
29
        // auto validation.
30
        $user->password_confirmation = array_get($input, 'password_confirmation');
31
32
        // Generate a random confirmation code
33
        $user->confirmation_code     = md5(uniqid(mt_rand(), true));
34
35
        // Save if valid. Password field will be hashed before save
36
        $this->save($user);
37
38
        return $user;
39
    }
40
41
    /**
42
     * Attempts to login with the given credentials.
43
     *
44
     * @param  array $input Array containing the credentials (email and password)
45
     *
46
     * @return  boolean Success?
47
     */
48
    public function login($input)
49
    {
50
        if (! isset($input['password'])) {
51
            $input['password'] = null;
52
        }
53
54
        return Confide::logAttempt($input, Config::get('confide::signup_confirm'));
55
    }
56
57
    /**
58
     * Checks if the credentials has been throttled by too
59
     * much failed login attempts
60
     *
61
     * @param  array $credentials Array containing the credentials (email and password)
0 ignored issues
show
Bug introduced by
There is no parameter named $credentials. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     *
63
     * @return  boolean Is throttled
64
     */
65
    public function isThrottled($input)
66
    {
67
        return Confide::isThrottled($input);
68
    }
69
70
    /**
71
     * Checks if the given credentials correponds to a user that exists but
72
     * is not confirmed
73
     *
74
     * @param  array $credentials Array containing the credentials (email and password)
0 ignored issues
show
Bug introduced by
There is no parameter named $credentials. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     *
76
     * @return  boolean Exists and is not confirmed?
77
     */
78
    public function existsButNotConfirmed($input)
79
    {
80
        $user = Confide::getUserByEmailOrUsername($input);
81
82
        if ($user) {
83
            $correctPassword = Hash::check(
84
                isset($input['password']) ? $input['password'] : false,
85
                $user->password
86
            );
87
88
            return (! $user->confirmed && $correctPassword);
89
        }
90
    }
91
92
    /**
93
     * Resets a password of a user. The $input['token'] will tell which user.
94
     *
95
     * @param  array $input Array containing 'token', 'password' and 'password_confirmation' keys.
96
     *
97
     * @return  boolean Success
98
     */
99
    public function resetPassword($input)
100
    {
101
        $result = false;
102
        $user   = Confide::userByResetPasswordToken($input['token']);
103
104
        if ($user) {
105
            $user->password              = $input['password'];
106
            $user->password_confirmation = $input['password_confirmation'];
107
            $result = $this->save($user);
108
        }
109
110
        // If result is positive, destroy token
111
        if ($result) {
112
            Confide::destroyForgotPasswordToken($input['token']);
113
        }
114
115
        return $result;
116
    }
117
118
    /**
119
     * Simply saves the given instance
120
     *
121
     * @param  User $instance
122
     *
123
     * @return  boolean Success
124
     */
125
    public function save(User $instance)
126
    {
127
        return $instance->save();
128
    }
129
}
130