TokenBroker::sendToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Fouladgar\MobileVerification\Tokens;
4
5
use Fouladgar\MobileVerification\Contracts\MustVerifyMobile;
6
use Fouladgar\MobileVerification\Exceptions\InvalidTokenException;
7
8
class TokenBroker implements TokenBrokerInterface
9
{
10
    /**
11
     * @var TokenRepositoryInterface
12
     */
13
    protected $tokenRepository;
14
15
    /**
16
     * Create a new token broker instance.
17
     *
18
     * @param TokenRepositoryInterface $tokenRepository
19
     */
20
    public function __construct(TokenRepositoryInterface $tokenRepository)
21
    {
22
        $this->tokenRepository = $tokenRepository;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function sendToken(MustVerifyMobile $user): void
29
    {
30
        $user->sendMobileVerifierNotification(
31
            $this->tokenRepository->create($user)
32
        );
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function verifyToken(MustVerifyMobile $user, $token): bool
39
    {
40
        throw_unless($this->tokenExists($user, $token), InvalidTokenException::class);
41
42
        $user->markMobileAsVerified();
43
44
        $this->tokenRepository->deleteExisting($user);
45
46
        return true;
47
    }
48
49
    /**
50
     * @param MustVerifyMobile $user
51
     * @param $token
52
     *
53
     * @return bool
54
     */
55
    protected function tokenExists(MustVerifyMobile $user, $token): bool
56
    {
57
        return $this->tokenRepository->exists($user, $token);
58
    }
59
}
60