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\ConfirmationTokenGenerated; |
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 $model User |
24
|
|
|
*/ |
25
|
11 |
|
public function __construct(User $users, Token $tokens) |
26
|
|
|
{ |
27
|
11 |
|
$this->users = $users; |
28
|
11 |
|
$this->tokens = $tokens; |
29
|
11 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Confirms a user if unconfirmed. |
33
|
|
|
* |
34
|
|
|
* @param $user User |
35
|
|
|
* |
36
|
|
|
* @return boolean |
37
|
|
|
*/ |
38
|
2 |
|
public function confirm(User $user) |
39
|
|
|
{ |
40
|
2 |
|
if (!$user->confirmed_at) { |
41
|
1 |
|
if ($this->update($user->id, ['confirmed_at' => Carbon::now()])) { |
42
|
1 |
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
1 |
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a single new user, generate a token and notify the user. |
50
|
|
|
* |
51
|
|
|
* @param $data array |
52
|
|
|
* |
53
|
|
|
* @return \JobApis\JobsToMail\Models\User |
54
|
|
|
*/ |
55
|
2 |
|
public function create($data = []) |
56
|
|
|
{ |
57
|
2 |
|
$user = $this->users->create($data); |
58
|
|
|
|
59
|
2 |
|
$this->sendConfirmationToken($user); |
60
|
|
|
|
61
|
2 |
|
return $user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Deletes a user. |
66
|
|
|
* |
67
|
|
|
* @param $id string |
68
|
|
|
* |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
1 |
|
public function delete($id = null) |
72
|
|
|
{ |
73
|
1 |
|
return $this->users->where('id', $id)->delete(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a single new user or returns an existing one by email |
78
|
|
|
* |
79
|
|
|
* @param $data array |
80
|
|
|
* |
81
|
|
|
* @return \JobApis\JobsToMail\Models\User |
82
|
|
|
*/ |
83
|
3 |
|
public function firstOrCreate($data = []) |
84
|
|
|
{ |
85
|
3 |
|
if ($user = $this->users->where('email', $data['email'])->first()) { |
86
|
|
|
// Resend the user a confirmation token if they haven't confirmed |
87
|
2 |
|
if (!$user->confirmed_at) { |
88
|
1 |
|
$this->sendConfirmationToken($user); |
89
|
|
|
} |
90
|
2 |
|
$user->existed = true; |
91
|
2 |
|
return $user; |
92
|
|
|
} |
93
|
1 |
|
return $this->create($data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Deletes a user's old tokens and generates a new one |
98
|
|
|
* |
99
|
|
|
* @param null $user_id |
100
|
|
|
* @param string $type |
101
|
|
|
* |
102
|
|
|
* @return Token |
103
|
|
|
*/ |
104
|
3 |
|
public function generateToken($user_id = null, $type = 'confirm') |
105
|
|
|
{ |
106
|
|
|
// Return the new one |
107
|
3 |
|
return $this->tokens->create([ |
108
|
3 |
|
'user_id' => $user_id, |
109
|
3 |
|
'type' => $type, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Retrieves a single record by ID |
115
|
|
|
* |
116
|
|
|
* @param $id string |
117
|
|
|
* @param $options array |
118
|
|
|
* |
119
|
|
|
* @return \JobApis\JobsToMail\Models\User |
120
|
|
|
*/ |
121
|
1 |
|
public function getById($id = null, $options = []) |
122
|
|
|
{ |
123
|
1 |
|
return $this->users->where('id', $id)->first(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieves a single record by Email |
128
|
|
|
* |
129
|
|
|
* @param $email string |
130
|
|
|
* @param $options array |
131
|
|
|
* |
132
|
|
|
* @return \JobApis\JobsToMail\Models\User |
133
|
|
|
*/ |
134
|
1 |
|
public function getByEmail($email = null, $options = []) |
135
|
|
|
{ |
136
|
1 |
|
return $this->users->where('email', $email)->first(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get Confirmation Token by token id if not expired |
141
|
|
|
* |
142
|
|
|
* @param string $token |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
1 |
|
public function getToken($token = null, $daysToExpire = 7) |
147
|
|
|
{ |
148
|
1 |
|
return $this->tokens |
149
|
1 |
|
->where('token', $token) |
150
|
1 |
|
->where('created_at', '>', Carbon::now()->subDays($daysToExpire)) |
151
|
1 |
|
->first(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Update a single user |
156
|
|
|
* |
157
|
|
|
* @param $id string |
158
|
|
|
* @param $data array |
159
|
|
|
* |
160
|
|
|
* @return boolean |
161
|
|
|
*/ |
162
|
2 |
|
public function update($id = null, $data = []) |
163
|
|
|
{ |
164
|
2 |
|
return $this->users->where('id', $id)->update($data); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Generates a new confirmation token and sends it to the user |
169
|
|
|
* |
170
|
|
|
* @param User $user |
171
|
|
|
* |
172
|
|
|
* @return Token |
173
|
|
|
*/ |
174
|
3 |
|
private function sendConfirmationToken(User $user) |
175
|
|
|
{ |
176
|
|
|
// Create a token |
177
|
3 |
|
$token = $this->generateToken($user->id, config('tokens.types.confirm')); |
178
|
|
|
// Email the token in link to the User |
179
|
3 |
|
$user->notify(new ConfirmationTokenGenerated($token)); |
180
|
|
|
|
181
|
3 |
|
return $token; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|