-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5226e05
commit 0af02cc
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zing\LaravelSms; | ||
|
||
use Illuminate\Contracts\Cache\Factory; | ||
|
||
class VerificationCodeManager | ||
{ | ||
protected $cacheManager; | ||
|
||
/** | ||
* VerificationCodeManager constructor. | ||
* | ||
* @param $cacheManager | ||
*/ | ||
public function __construct(Factory $cacheManager) | ||
{ | ||
$this->cacheManager = $cacheManager; | ||
} | ||
|
||
protected function getPrefixedKey($number) | ||
{ | ||
return config('sms.verification.prefix') . $number; | ||
} | ||
|
||
public function verify($number, $code) | ||
{ | ||
return $code === $this->cacheManager->get($this->getPrefixedKey($number)); | ||
} | ||
|
||
public function issue($number) | ||
{ | ||
$length = config('sms.verification.length'); | ||
$code = random_int(10 ** ($length - 1), (10 ** $length) - 1); | ||
$this->cacheManager->set($this->getPrefixedKey($number), $code, 600); | ||
|
||
return $code; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zing\LaravelSms\Tests; | ||
|
||
use Zing\LaravelSms\SmsNumber; | ||
use Zing\LaravelSms\VerificationCodeManager; | ||
|
||
class VerificationCodeManagerTest extends TestCase | ||
{ | ||
protected $manager; | ||
|
||
protected function getEnvironmentSetUp($app): void | ||
{ | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->manager = app(VerificationCodeManager::class); | ||
} | ||
|
||
public function testIssue(): void | ||
{ | ||
$code = $this->manager->issue('18888888888'); | ||
self::assertSame(config('sms.verification.length'), strlen((string) $code)); | ||
} | ||
|
||
public function testVerify(): void | ||
{ | ||
$code = $this->manager->issue(new SmsNumber('18888888888')); | ||
self::assertTrue($this->manager->verify(new SmsNumber('18888888888'), $code)); | ||
} | ||
} |