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

OtpService::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/*
3
 * Copyright (c) 2021. Hilmi Erdem Keren
4
 * license MIT
5
 */
6
7
namespace Erdemkeren\Otp;
8
9
use UnexpectedValueException;
10
use Erdemkeren\Otp\Contracts\FormatContract;
11
use Erdemkeren\Otp\Contracts\EncryptorContract;
12
use Erdemkeren\Otp\Contracts\FormatManagerContract;
13
use Erdemkeren\Otp\Contracts\TokenRepositoryContract;
14
use Illuminate\Contracts\Auth\Authenticatable;
15
16
class OtpService
17
{
18
    public function __construct(
19
        private FormatManagerContract   $manager,
20
        private EncryptorContract       $encryptor,
21
        private TokenRepositoryContract $repository,
22
    ) {
23
        //
24
    }
25
26
    public function retrieveByCipherText(string $cipherText): ?OtpToken
27
    {
28
        return $this->repository->retrieveByCipherText($cipherText);
29
    }
30
31
    public function create(int|string $authenticableId, string $format = 'default'): OtpToken
32
    {
33
        $format = $this->getFormat($format);
34
35
        return tap(new OtpToken([
36
            'format' => $format->name(),
37
            'plain_text' => $plainText = $format->generator()(),
38
            'cipher_text' => $this->encryptor->encrypt($plainText),
39
            'expiry_time' => 300,
40
            'authenticable_id' => $authenticableId,
41
        ]), fn (OtpToken $otpToken) => $this->repository->persist($otpToken));
42
    }
43
44
    public function save(OtpToken $token): bool
45
    {
46
        return $this->repository->persist($token);
47
    }
48
49
    public function extend(OtpToken $token, int $secs): OtpToken
50
    {
51
        $extended = $token->extend($secs);
52
53
        $this->repository->persist($extended);
54
55
        return $extended;
56
    }
57
58
    public function refresh(OtpToken $token): OtpToken
59
    {
60
        $refreshed = $token->refresh();
61
62
        $this->repository->persist($refreshed);
63
64
        return $refreshed;
65
    }
66
67
    public function invalidate(OtpToken $token): OtpToken
68
    {
69 8
        $invalidated = $token->invalidate();
70
71
        $this->repository->persist($invalidated);
72
73
        return $invalidated;
74
    }
75
76 8
    public function addFormat(FormatContract $format): void
77 8
    {
78 8
        $this->manager->register($format);
79 8
    }
80
81 8
    public function sendOtpNotification(object $notifiable, OtpToken $token): void
82 1
    {
83 1
        $notifiable->notify(
84
            $this->getFormat($token->format())->createNotification($token)
85
        );
86
    }
87 8
88 8
    public function sendNewOtp(Authenticatable $user): void
89 1
    {
90 1
        $token = $this->create($user->getAuthIdentifier());
0 ignored issues
show
Bug introduced by
$user->getAuthIdentifier() of type null is incompatible with the type integer|string expected by parameter $authenticableId of Erdemkeren\Otp\OtpService::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        $token = $this->create(/** @scrutinizer ignore-type */ $user->getAuthIdentifier());
Loading history...
Bug introduced by
Are you sure the usage of $user->getAuthIdentifier() targeting Erdemkeren\Otp\Test\Noti...le::getAuthIdentifier() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
92
        if (! method_exists($user, 'notify')) {
93
            throw new UnexpectedValueException(
94 8
                'The otp owner should be an instance of notifiable or implement the notify method.'
95 1
            );
96 1
        }
97
98
        $this->sendOtpNotification($user, $token);
99
    }
100 8
101 8
    private function getFormat(string $format): FormatContract
102
    {
103
        return $this->manager->get($format);
104
    }
105
}
106