Passed
Pull Request — master (#81)
by Zing
06:27
created

VerificationCodeManager   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPrefixedKey() 0 3 1
A issue() 0 7 1
A __construct() 0 3 1
A verify() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSms;
6
7
use Illuminate\Contracts\Cache\Factory;
8
9
class VerificationCodeManager
10
{
11
    protected $cacheManager;
12
13
    /**
14
     * VerificationCodeManager constructor.
15
     *
16
     * @param $cacheManager
17
     */
18 2
    public function __construct(Factory $cacheManager)
19
    {
20 2
        $this->cacheManager = $cacheManager;
21 2
    }
22
23 2
    protected function getPrefixedKey($number)
24
    {
25 2
        return config('sms.verification.prefix') . $number;
26
    }
27
28 1
    public function verify($number, $code)
29
    {
30 1
        return $code === $this->cacheManager->get($this->getPrefixedKey($number));
31
    }
32
33 2
    public function issue($number)
34
    {
35 2
        $length = config('sms.verification.length');
36 2
        $code = random_int(10 ** ($length - 1), (10 ** $length) - 1);
37 2
        $this->cacheManager->set($this->getPrefixedKey($number), $code, 600);
38
39 2
        return $code;
40
    }
41
}
42