Passed
Pull Request — master (#7)
by Koen
04:19
created

EmailDispensary   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 7 1
A __construct() 0 3 1
A verify() 0 3 1
A dispense() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth;
6
7
use App\Auth\Dispensary\Dispensary;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use function class_basename;
10
11
final class EmailDispensary
12
{
13
    const TTL = 1800;
14
    const CHARS = 128;
15
16
    /**
17
     * @var \App\Auth\Dispensary\Dispensary
18
     */
19
    private Dispensary $dispensary;
20
21 7
    public function __construct(Dispensary $dispensary)
22
    {
23 7
        $this->dispensary = $dispensary;
24 7
    }
25
26 4
    public function dispense(Authenticatable $user, string $email): string
27
    {
28 4
        return $this->dispensary->dispense($this->getKey($user, $email), self::TTL, self::CHARS);
29
    }
30
31
    /**
32
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
33
     * @param  string                                     $email
34
     * @param  string                                     $token
35
     * @return bool
36
     * @throws \App\Auth\Dispensary\Exceptions\TokenExpiredException
37
     * @throws \Psr\SimpleCache\InvalidArgumentException
38
     */
39 3
    public function verify(Authenticatable $user, string $email, string $token): bool
40
    {
41 3
        return $this->dispensary->verify($this->getKey($user, $email), $token);
42
    }
43
44 4
    private function getKey(Authenticatable $user, string $email): string
45
    {
46 4
        return implode('_', [
47 4
            class_basename($user),
48 4
            $user->getAuthIdentifier(),
49 4
            'email',
50 4
            $email,
51
        ]);
52
    }
53
}
54