Completed
Push — master ( eaf36c...f92548 )
by Marcel
03:19
created

ConfirmationManager::removeOldConfirmations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Security\EmailConfirmation;
4
5
use App\Entity\EmailConfirmation;
6
use App\Entity\User;
7
use App\Repository\EmailConfirmationRepositoryInterface;
8
use App\Repository\UserRepositoryInterface;
9
use SchulIT\CommonBundle\Helper\DateHelper;
10
use Swift_Mailer;
11
use Symfony\Contracts\Translation\TranslatorInterface;
12
use Twig\Environment;
13
14
class ConfirmationManager {
15
16
    private const Lifetime = '+2 hours';
17
18
    private $from;
19
    private $dateHelper;
20
    private $repository;
21
    private $userRepository;
22
    private $translator;
23
    private $mailer;
24
    private $twig;
25
26 1
    public function __construct(string $from, DateHelper $dateHelper, EmailConfirmationRepositoryInterface $repository, UserRepositoryInterface $userRepository,
27
                                TranslatorInterface $translator, Swift_Mailer $mailer, Environment $twig) {
28 1
        $this->from = $from;
29 1
        $this->dateHelper = $dateHelper;
30 1
        $this->repository = $repository;
31 1
        $this->userRepository = $userRepository;
32 1
        $this->translator = $translator;
33 1
        $this->mailer = $mailer;
34 1
        $this->twig = $twig;
35 1
    }
36
37 1
    public function hasConfirmation(User $user): bool {
38 1
        return $this->repository->findOneByUser($user) !== null;
39
    }
40
41
    public function newConfirmation(User $user, string $email): void {
42
        $confirmation = $this->repository->findOneByUser($user);
43
44
        if($confirmation !== null) {
45
            $this->repository->remove($confirmation);
46
        }
47
48
        $confirmation = (new EmailConfirmation())
49
            ->setUser($user)
50
            ->setEmailAddress($email)
51
            ->setValidUntil($this->dateHelper->getNow()->modify(static::Lifetime));
52
53
        do {
54
            $confirmation->setToken(bin2hex(openssl_random_pseudo_bytes(64)));
55
        } while($this->repository->findOneByToken($confirmation->getToken()) !== null);
56
57
        $this->repository->persist($confirmation);
58
59
        $content = $this->twig
60
            ->render('mail/email_confirmation.html.twig', [
61
                'token' => $confirmation->getToken(),
62
                'firstname' => $user->getFirstname(),
63
                'lastname' => $user->getLastname(),
64
                'expiry_date' => $confirmation->getValidUntil()
65
            ]);
66
67
        $message = (new \Swift_Message())
68
            ->setSubject($this->translator->trans('registration.title', [], 'mail'))
69
            ->setTo($user->getEmail())
70
            ->setFrom($this->from)
71
            ->setBody($content);
72
73
        $this->mailer->send($message);
74
    }
75
76
    /**
77
     * @param string $token
78
     * @throws TokenNotFoundException
79
     */
80
    public function confirm(string $token): void {
81
        $confirmation = $this->repository->findOneByToken($token);
82
83
        if($confirmation === null) {
84
            throw new TokenNotFoundException($token);
85
        }
86
87
        $user = $confirmation->getUser();
88
        $user->setEmail($confirmation->getEmailAddress());
89
90
        $this->userRepository->persist($user);
91
        $this->repository->remove($confirmation);
92
    }
93
94
    public function removeOldConfirmations(): int {
95
        return $this->repository->removeExpired($this->dateHelper->getNow());
96
    }
97
}