Completed
Pull Request — master (#11)
by Karl
02:49
created

UserRepository::confirm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php namespace JobApis\JobsToMail\Repositories;
2
3
use Carbon\Carbon;
4
use JobApis\JobsToMail\Models\Token;
5
use JobApis\JobsToMail\Models\User;
6
use JobApis\JobsToMail\Notifications\TokenGenerated;
7
8
class UserRepository implements Contracts\UserRepositoryInterface
9
{
10
    /**
11
     * @var Token model
12
     */
13
    public $tokens;
14
15
    /**
16
     * @var User model
17
     */
18
    public $users;
19
20
    /**
21
     * UserRepository constructor.
22
     *
23
     * @param User $model
0 ignored issues
show
Bug introduced by
There is no parameter named $model. 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...
24
     */
25 8
    public function __construct(User $users, Token $tokens)
26
    {
27 8
        $this->users = $users;
28 8
        $this->tokens = $tokens;
29 8
    }
30
31
    /**
32
     * Confirms a user's email and activates their account.
33
     *
34
     * @param $token string
35
     *
36
     * @return boolean
37
     */
38 2
    public function confirm($token = null)
39
    {
40 2
        $tokenObject = $this->getUnexpiredConfirmationToken($token);
41 2
        if ($tokenObject) {
42 1
            if ($this->update($tokenObject->user_id, ['confirmed_at' => Carbon::now()])) {
43 1
                return true;
44
            }
45
        }
46 1
        return false;
47
    }
48
49
    /**
50
     * Creates a single new user, generate a token and notify the user.
51
     *
52
     * @param $data array
53
     *
54
     * @return \JobApis\JobsToMail\Models\User
55
     */
56 2
    public function create($data = [])
57
    {
58 2
        $user = $this->users->create($data);
59
60 2
        $this->sendConfirmationToken($user);
61
62 2
        return $user;
63
    }
64
65
    /**
66
     * Creates a single new user or returns an existing one by email
67
     *
68
     * @param $data array
69
     *
70
     * @return \JobApis\JobsToMail\Models\User
71
     */
72 3
    public function firstOrCreate($data = [])
73
    {
74 3
        if ($user = $this->users->where('email', $data['email'])->first()) {
75
            // Resend the user a confirmation token if they haven't confirmed
76 2
            if (!$user->confirmed_at) {
77 1
                $this->sendConfirmationToken($user);
78
            }
79 2
            $user->existed = true;
80 2
            return $user;
81
        }
82 1
        return $this->create($data);
83
    }
84
85
    /**
86
     * Retrieves a single record by ID
87
     *
88
     * @param $id string
89
     * @param $options array
90
     *
91
     * @return \JobApis\JobsToMail\Models\User
92
     */
93 1
    public function getById($id = null, $options = [])
94
    {
95 1
        return $this->users->where('id', $id)->first();
96
    }
97
98
    /**
99
     * Update a single user
100
     *
101
     * @param $id string
102
     * @param $data array
103
     *
104
     * @return boolean
105
     */
106 1
    public function update($id = null, $data = [])
107
    {
108 1
        return $this->users->where('id', $id)->update($data);
109
    }
110
111
    /**
112
     * Deletes a user.
113
     *
114
     * @param $id string
115
     *
116
     * @return boolean
117
     */
118 1
    public function unsubscribe($id = null)
119
    {
120 1
        return $this->users->where('id', $id)->delete();
121
    }
122
123
    /**
124
     * Get Confirmation Token by token id if not expired
125
     *
126
     * @param string $token
127
     *
128
     * @return mixed
129
     */
130 2
    private function getUnexpiredConfirmationToken($token = null, $daysToExpire = 30)
131
    {
132 2
        return $this->tokens
133 2
            ->where('token', $token)
134 2
            ->where('type', config('tokens.types.confirm'))
135 2
            ->where('created_at', '>', Carbon::now()->subDays($daysToExpire))
136 2
            ->first();
137
    }
138
139
    /**
140
     * Generates and returns a token for a specific User Id
141
     *
142
     * @param null $user_id
143
     * @param string $type
144
     *
145
     * @return Token
146
     */
147 3
    private function generateToken($user_id = null, $type = 'confirm')
148
    {
149 3
        return $this->tokens->create([
150 3
            'user_id' => $user_id,
151 3
            'type' => $type,
152
        ]);
153
    }
154
155
    /**
156
     * Generates a new confirmation token and sends it to the user
157
     *
158
     * @param User $user
159
     *
160
     * @return Token
161
     */
162 3
    private function sendConfirmationToken(User $user)
163
    {
164
        // Create a token
165 3
        $token = $this->generateToken($user->id, config('tokens.types.confirm'));
166
        // Email the token in link to the User
167 3
        $user->notify(new TokenGenerated($token));
168
169 3
        return $token;
170
    }
171
}
172