Completed
Push — master ( c9d345...34b48c )
by Valentyn
03:02 queued 01:25
created

ConfirmationTokenService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Service\User;
5
6
use App\Entity\ConfirmationToken;
7
use App\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
}