Completed
Push — master ( 15189e...f90573 )
by Valentyn
13s
created

ConfirmationTokenService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEmailConfirmationToken() 0 7 1
A getToken() 0 13 2
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Service;
5
6
use App\Users\Entity\ConfirmationToken;
7
use App\Users\Entity\User;
8
use Doctrine\ORM\EntityManagerInterface;
9
10
class ConfirmationTokenService
11
{
12
    private $entityManager;
13
14 5
    public function __construct(EntityManagerInterface $entityManager)
15
    {
16 5
        $this->entityManager = $entityManager;
17 5
    }
18
19 5
    public function getEmailConfirmationToken(User $user): ConfirmationToken
20
    {
21 5
        $expires_at = new \DateTimeImmutable();
22 5
        $expires_at->modify('+14 days');
23
24 5
        return $this->getToken($user, ConfirmationToken::TYPE_CONFIRM_EMAIl, $expires_at);
25
    }
26
27 5
    private function getToken(User $user, string $type, \DateTimeInterface $expires_at = null): ConfirmationToken
28
    {
29 5
        $confirmationToken = new ConfirmationToken($user, $type);
30
31 5
        if ($expires_at instanceof \DateTimeInterface) {
32 5
            $confirmationToken->setExpiresAt($expires_at);
33
        }
34
35 5
        $this->entityManager->persist($confirmationToken);
36 5
        $this->entityManager->flush();
37
38 5
        return $confirmationToken;
39
    }
40
}