Passed
Pull Request — master (#5831)
by
unknown
47:54 queued 24:36
created

ValidationTokenHelper::generateLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Entity\ValidationToken;
10
use Chamilo\CoreBundle\Repository\ValidationTokenRepository;
11
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12
13
class ValidationTokenHelper
14
{
15
    public function __construct(
16
        private readonly ValidationTokenRepository $tokenRepository,
17
        private readonly UrlGeneratorInterface $urlGenerator,
18
    ) {}
19
20
    public function generateLink(int $type, int $resourceId): string
21
    {
22
        $token = new ValidationToken($type, $resourceId);
23
        $this->tokenRepository->save($token, true);
24
25
        return $this->urlGenerator->generate('validate_token', [
26
            'type' => $this->getTypeString($type),
27
            'hash' => $token->getHash(),
28
        ], UrlGeneratorInterface::ABSOLUTE_URL);
29
    }
30
31
    public function getTypeId(string $type): int
32
    {
33
        return match ($type) {
34
            'ticket' => 1,
35
            'user' => 2,
36
            default => throw new \InvalidArgumentException('Unrecognized validation type'),
37
        };
38
    }
39
40
    private function getTypeString(int $type): string
41
    {
42
        return match ($type) {
43
            1 => 'ticket',
44
            2 => 'user',
45
            default => throw new \InvalidArgumentException('Unrecognized validation type'),
46
        };
47
    }
48
}
49