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

EmailDispensary::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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