Dispensary   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 41
ccs 14
cts 14
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dispense() 0 7 1
A __construct() 0 4 1
A generateToken() 0 3 1
A verify() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth\Dispensary;
6
7
use App\Auth\Dispensary\Exceptions\TokenExpiredException;
8
use Illuminate\Contracts\Hashing\Hasher;
9
use Illuminate\Support\Str;
10
11
final class Dispensary
12
{
13
    private Repository $repository;
14
15
    private Hasher $hasher;
16
17
    public function __construct(Repository $repository, Hasher $hasher)
18 17
    {
19
        $this->repository = $repository;
20 17
        $this->hasher = $hasher;
21 17
    }
22 17
23
    public function dispense(string $key, int $ttl, int $chars): string
24 9
    {
25
        $token = $this->generateToken($chars);
26 9
27
        $this->repository->put($key, $this->hasher->make($token), $ttl);
28 9
29
        return $token;
30 9
    }
31
32
    /**
33
     * @param  string $key
34
     * @param  string $token
35
     * @return bool
36
     * @throws \App\Auth\Dispensary\Exceptions\TokenExpiredException
37
     */
38
    public function verify(string $key, string $token): bool
39
    {
40 7
        $hashedToken = $this->repository->get($key);
41
42 7
        if (null === $hashedToken) {
43
            throw new TokenExpiredException();
44 7
        }
45 2
46
        return $this->hasher->check($token, $hashedToken);
47
    }
48 5
49
    private function generateToken(int $chars): string
50
    {
51 9
        return Str::random($chars);
52
    }
53
}
54