| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\Activation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Class ActivationRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package App\Repositories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ActivationService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Activation email template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $activationEmailTemplate = 'emails.activation'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function createActivation(User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $user->activation()->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $hash = Hash::make(str_random(Activation::ACTIVATION_HASH_LENGTH)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $expired = Carbon::now()->addDay(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Activation::insert([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'user_id' => $user->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'token'   => $hash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'expired' => $expired, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return Activation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function getActivation(User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $user->activation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return Activation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getActivationByToken(string $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return Activation::where('token', $token)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     public function verifyEmail(string $token) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         $activation = Activation::where('token', $token)->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         if ($activation) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $user = $activation->user; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $user->activated = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $user->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $activation->delete(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             return $user; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function deleteOldActivations() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $date = Carbon::now()->subDays(Activation::EXPIRE_DAYS); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $activations = Activation::where('expired', '<', $date->toDateTimeString())->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if (!$activations->isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             User::destroy($activations->pluck('user_id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param User $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $hash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function sendActivationEmail(User $user, string $hash) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return Mail::send( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             $this->activationEmailTemplate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ['hash' => $hash], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             function ($message) use ($user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $message->to($user->email, $user->usename)->subject('Laravel api boilerplate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function resendActivationEmail($email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $user = User::where('email', '=', $email)->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if ($user->activated) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $hash = $this->createActivation($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->sendActivationEmail($user, $hash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |  |