TokenBroker   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
c 0
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A verifyToken() 0 9 1
A sendToken() 0 4 1
A tokenExists() 0 3 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