Skip to content

Commit

Permalink
Merge pull request alaatv#15 from MohamadSH/master
Browse files Browse the repository at this point in the history
mobile verification
  • Loading branch information
MohamadSH authored Sep 26, 2019
2 parents 5d5964f + 5d77e77 commit 6a24688
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
28 changes: 28 additions & 0 deletions app/Events/MobileVerificationCodeGenerated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Events;

use App\User;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;

class MobileVerificationCodeGenerated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $generationTime;

/**
* Create a new event instance.
*
* @param User $user
* @param $generationTime
*/
public function __construct(User $user , $generationTime=null)
{
$this->user = $user;
$this->generationTime = $generationTime;
}

}
3 changes: 2 additions & 1 deletion app/Events/MobileVerified.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace App\Events;

use App\Classes\Verification\MustVerifyMobileNumber;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MobileVerified
{
use SerializesModels;
use Dispatchable, SerializesModels;

/**
* The verified user.
Expand Down
26 changes: 26 additions & 0 deletions app/Listeners/MobileVerificationCodeGeneratedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Listeners;

use App\Events\MobileVerificationCodeGenerated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class MobileVerificationCodeGeneratedListener
{
use InteractsWithQueue;

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(MobileVerificationCodeGenerated $event)
{
$timeStamp = $event->generationTime;
$user = $event->user;
// Carbon::createFromTimestamp($timeStamp);
Log::info('MobileVerificationCodeGeneratedListener : '.$user->id.' - '.$timeStamp);
}
}
6 changes: 5 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Providers;

use App\Events\CountOption;
use App\Events\MobileVerificationCodeGenerated;
use App\Events\MobileVerified;
use App\Listeners\CountOptionListener;
use App\Listeners\MobileVerificationCodeGeneratedListener;
use App\Listeners\MobileVerifiedListener;
use App\Listeners\SendMobileVerificationNotification;
use Illuminate\Auth\Events\Registered;
Expand All @@ -28,8 +30,10 @@ class EventServiceProvider extends ServiceProvider
],
CountOption::class => [
CountOptionListener::class
],
MobileVerificationCodeGenerated::class => [
MobileVerificationCodeGeneratedListener::class
]

];

/**
Expand Down
31 changes: 25 additions & 6 deletions app/Traits/MustVerifyMobileNumberTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

namespace App\Traits;

use App\Events\MobileVerificationCodeGenerated;
use App\Notifications\MobileVerified;
use App\Notifications\VerifyMobile;
use Illuminate\Support\Facades\Cache;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

trait MustVerifyMobileNumberTrait
{
Expand All @@ -21,7 +23,7 @@ trait MustVerifyMobileNumberTrait
*/
public function hasVerifiedMobile()
{
return $this->mobile_verified_at !== null;
return ! is_null($this->mobile_verified_at);
}

/**
Expand All @@ -44,10 +46,10 @@ public function markMobileAsVerified()
public function sendMobileVerificationNotification()
{
if ($this->setMobileVerificationCode()) {
$this->notify(new VerifyMobile());
// $this->notify(new VerifyMobile());
}
}

/**
* generate a verification code for given user
*
Expand All @@ -56,11 +58,28 @@ public function sendMobileVerificationNotification()
*/
public function setMobileVerificationCode(): bool
{
$verificationCode = random_int(10000, 99999);
Log::info('before event');
event(new MobileVerificationCodeGenerated($this , Carbon::now('Asia/Tehran')));
return true;

return $this->forceFill([

$verificationCode = $this->getMobileVerificationCode();
if(isset($verificationCode)) {
return true;
}


$verificationCode = random_int(10000, 99999);
$result = $this->forceFill([
'mobile_verified_code' => $verificationCode,
])->save();

if($result){
event(new MobileVerificationCodeGenerated($this , Carbon::now('Asia/Tehran')));

return true;
}
return false;
}

/**
Expand Down

0 comments on commit 6a24688

Please sign in to comment.