Passed
Push — master ( e717c7...e31763 )
by
unknown
08:28 queued 13s
created

ValidationTokenHelper::generateLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 10
rs 10
c 2
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
    // Define constants for the types
16
    public const TYPE_TICKET = 1;
17
    public const TYPE_USER = 2;
18
19
    public function __construct(
20
        private readonly ValidationTokenRepository $tokenRepository,
21
        private readonly UrlGeneratorInterface $urlGenerator,
22
    ) {}
23
24
    public function generateLink(int $type, int $resourceId): string
25
    {
26
        $token = new ValidationToken($type, $resourceId);
27
        $this->tokenRepository->save($token, true);
28
29
        // Generate a validation link with the token's hash
30
        return $this->urlGenerator->generate('validate_token', [
31
            'type' => $this->getTypeString($type),
32
            'hash' => $token->getHash(),
33
        ], UrlGeneratorInterface::ABSOLUTE_URL);
34
    }
35
36
    public function getTypeId(string $type): int
37
    {
38
        return match ($type) {
39
            'ticket' => self::TYPE_TICKET,
40
            'user' => self::TYPE_USER,
41
            default => throw new \InvalidArgumentException('Unrecognized validation type'),
42
        };
43
    }
44
45
    private function getTypeString(int $type): string
46
    {
47
        return match ($type) {
48
            self::TYPE_TICKET => 'ticket',
49
            self::TYPE_USER => 'user',
50
            default => throw new \InvalidArgumentException('Unrecognized validation type'),
51
        };
52
    }
53
}
54