Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. <?php
  2. namespace App;
  3.  
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Connection;
  6.  
  7. class ActivationRepository{
  8.  
  9. protected $db;
  10. protected $table = "user_activations";
  11.  
  12. public function __construct(Connection $db){
  13. $this->db = $db;
  14. }
  15.  
  16. protected function getToken(){
  17. return hash_hmac('sha256', str_random(40), config('app.key'));
  18. }
  19.  
  20. public function createActivation($user){
  21. $activation = $this->getActivation($user);
  22. if(!$activation){
  23. return $this->createToken($user);
  24. }
  25. return $this->regenerateToken($user);
  26. }
  27.  
  28. private function regenerateToken($user){
  29. $token = $this->getToken();
  30. $this->db->table($this->table)->where('user_id', $user->id)->update([
  31. 'token' => $token,
  32. 'created_at' => new Carbon()
  33. ]);
  34. return $token;
  35. }
  36.  
  37. private function createToken($user){
  38. $token = $this->getToken();
  39. $this->db->table($this->table)->insert([
  40. 'user_id' => $user->id,
  41. 'token' => $token,
  42. 'created_at' => new Carbon()
  43. ]);
  44. return $token;
  45. }
  46.  
  47. public function getActivation($user){
  48. return $this->db->table($this->table)->where('user_id', $user->id)->first();
  49. }
  50.  
  51. public function getActivationByToken($token){
  52. return $this->db->table($this->table)->where('token', $token)->first();
  53. }
  54.  
  55. public function deleteActivation($token){
  56. return $this->db->table($this->table)->where('token', $token)->delete();
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement