Passed
Pull Request — master (#20)
by Hilmi Erdem
10:18
created

OtpService::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 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
    /**
29
     * Create a new token and get it.
30
     *
31
     * @param int|string $authenticableId
32
     * @param string     $generator
33
     *
34
     * @return OtpToken
35
     */
36
    public function create(int|string $authenticableId, string $generator = 'default'): OtpToken
37
    {
38
        $plainText = $this->getPasswordGenerator($generator)();
39
        $cipherText = $this->encryptor->encrypt($plainText);
40
41
        $token = new OtpToken([
42
            'plain_text' => $plainText,
43
            'cipher_text' => $cipherText,
44
            'expiry_time' => 300,
45
            'authenticable_id' => $authenticableId,
46
        ]);
47
48
        $this->tokenRepository->persist($token);
49
50
        return $token;
51
    }
52
53
    /**
54
     * Save the given token to the storage.
55
     *
56
     * @param OtpToken $token
57
     *
58
     * @return bool
59
     */
60
    public function save(OtpToken $token): bool
61
    {
62
        return $this->tokenRepository->persist($token);
63
    }
64
65
    /**
66
     * Extend the given token and get the extended instance.
67
     *
68
     * @param OtpToken $token
69 8
     * @param int      $secs
70
     *
71
     * @return OtpToken
72
     */
73
    public function extend(OtpToken $token, int $secs): OtpToken
74
    {
75
        $extended = $token->extend($secs);
76 8
77 8
        $this->tokenRepository->persist($extended);
78 8
79 8
        return $extended;
80
    }
81 8
82 1
    /**
83 1
     * Refresh the given token and get the refreshed instance.
84
     *
85
     * @param OtpToken $token
86
     *
87 8
     * @return OtpToken
88 8
     */
89 1
    public function refresh(OtpToken $token): OtpToken
90 1
    {
91
        $refreshed = $token->refresh();
92
93
        $this->tokenRepository->persist($refreshed);
94 8
95 1
        return $refreshed;
96 1
    }
97
98
    /**
99
     * Invalidate the given token and get the invalidated instance.
100 8
     *
101 8
     * @param OtpToken $token
102
     *
103
     * @return OtpToken
104
     */
105
    public function invalidate(OtpToken $token): OtpToken
106
    {
107
        $invalidated = $token->invalidate();
108
109
        $this->tokenRepository->persist($invalidated);
110
111
        return $invalidated;
112 1
    }
113
114 1
    /**
115
     * Add a new password generator implementation.
116 1
     *
117
     * @param string $name
118
     * @param string|callable $generator
119
     *
120
     * @return void
121
     */
122
    public function addPasswordGenerator(string $name, string|callable $generator): void
123
    {
124 1
        $this->manager->register($name, $generator);
125
    }
126 1
127 1
    /**
128
     * Get the token generator by the given name.
129
     *
130
     * @param string $name
131
     *
132
     * @return callable
133
     * @throws UnregisteredGeneratorException
134
     */
135
    private function getPasswordGenerator(string $generator): callable
136
    {
137 2
        return $this->manager->get($generator);
138
    }
139
}
140