Passed
Pull Request — master (#20)
by Hilmi Erdem
13:59 queued 07:56
created

OtpService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 114
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 15 1
A getPasswordGenerator() 0 3 1
A refresh() 0 7 1
A save() 0 3 1
A addPasswordGenerator() 0 3 1
A extend() 0 7 1
A __construct() 0 5 1
A invalidate() 0 7 1
1
<?php
2
/*
3
 * Copyright (c) 2021. Hilmi Erdem Keren
4
 * license MIT
5
 */
6
7
namespace Erdemkeren\Otp;
8
9
use Erdemkeren\Otp\Contracts\EncryptorContract;
10
use Erdemkeren\Otp\Contracts\GeneratorManagerContract;
11
use Erdemkeren\Otp\Contracts\TokenRepositoryContract;
12
use Erdemkeren\Otp\Exceptions\UnregisteredGeneratorException;
13
14
/**
15
 * Class OtpService.
16
 */
17
class OtpService
18
{
19
    public function __construct(
20
        private GeneratorManagerContract $manager,
21
        private EncryptorContract $encryptor,
22
        private TokenRepositoryContract $tokenRepository,
23
    ) {
24
        //
25
    }
26
27
    /**
28
     * Create a new token and get it.
29
     *
30
     * @param  int|string  $authenticableId
31
     * @param  string  $generator
32
     * @return OtpToken
33
     */
34
    public function create(int|string $authenticableId, string $generator = 'default'): OtpToken
35
    {
36
        $plainText = $this->getPasswordGenerator($generator)();
37
        $cipherText = $this->encryptor->encrypt($plainText);
38
39
        $token = new OtpToken([
40
            'plain_text' => $plainText,
41
            'cipher_text' => $cipherText,
42
            'expiry_time' => 300,
43
            'authenticable_id' => $authenticableId,
44
        ]);
45
46
        $this->tokenRepository->persist($token);
47
48
        return $token;
49
    }
50
51
    /**
52
     * Save the given token to the storage.
53
     *
54
     * @param  OtpToken  $token
55
     * @return bool
56
     */
57
    public function save(OtpToken $token): bool
58
    {
59
        return $this->tokenRepository->persist($token);
60
    }
61
62
    /**
63
     * Extend the given token and get the extended instance.
64
     *
65
     * @param  OtpToken  $token
66
     * @param  int  $secs
67
     * @return OtpToken
68
     */
69 8
    public function extend(OtpToken $token, int $secs): OtpToken
70
    {
71
        $extended = $token->extend($secs);
72
73
        $this->tokenRepository->persist($extended);
74
75
        return $extended;
76 8
    }
77 8
78 8
    /**
79 8
     * Refresh the given token and get the refreshed instance.
80
     *
81 8
     * @param  OtpToken  $token
82 1
     * @return OtpToken
83 1
     */
84
    public function refresh(OtpToken $token): OtpToken
85
    {
86
        $refreshed = $token->refresh();
87 8
88 8
        $this->tokenRepository->persist($refreshed);
89 1
90 1
        return $refreshed;
91
    }
92
93
    /**
94 8
     * Invalidate the given token and get the invalidated instance.
95 1
     *
96 1
     * @param  OtpToken  $token
97
     * @return OtpToken
98
     */
99
    public function invalidate(OtpToken $token): OtpToken
100 8
    {
101 8
        $invalidated = $token->invalidate();
102
103
        $this->tokenRepository->persist($invalidated);
104
105
        return $invalidated;
106
    }
107
108
    /**
109
     * Add a new password generator implementation.
110
     *
111
     * @param  string  $name
112 1
     * @param  string|callable  $generator
113
     * @return void
114 1
     */
115
    public function addPasswordGenerator(string $name, string|callable $generator): void
116 1
    {
117
        $this->manager->register($name, $generator);
118
    }
119
120
    /**
121
     * Get the token generator by the given name.
122
     *
123
     * @param  string  $name
124 1
     * @return callable
125
     *
126 1
     * @throws UnregisteredGeneratorException
127 1
     */
128
    private function getPasswordGenerator(string $generator): callable
129
    {
130
        return $this->manager->get($generator);
131
    }
132
}
133