Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
09:19 queued 13s
created

AccessUrlHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isMultipleEnabled() 0 3 1
A getCurrent() 0 18 3
A getFirstAccessUrl() 0 5 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\Routing\RouterInterface;
14
15
class AccessUrlHelper
16
{
17
    public function __construct(
18
        private readonly AccessUrlRepository $accessUrlRepository,
19
        private readonly ParameterBagInterface $parameterBag,
20
        private readonly RouterInterface $router,
21
    ) {}
22
23
    public function isMultipleEnabled(): bool
24
    {
25
        return 1 === (int) $this->parameterBag->get('multiple_access_url');
26
    }
27
28
    public function getFirstAccessUrl(): AccessUrl
29
    {
30
        $urlId = $this->accessUrlRepository->getFirstId();
31
32
        return $this->accessUrlRepository->find($urlId);
33
    }
34
35
    public function getCurrent(): AccessUrl
36
    {
37
        static $accessUrl;
38
39
        if (!empty($accessUrl)) {
40
            return $accessUrl;
41
        }
42
43
        $accessUrl = $this->getFirstAccessUrl();
44
45
        if ($this->isMultipleEnabled()) {
46
            $url = $this->router->generate('index', [], UrlGeneratorInterface::ABSOLUTE_URL);
47
48
            /** @var AccessUrl $accessUrl */
49
            $accessUrl = $this->accessUrlRepository->findOneBy(['url' => $url]);
50
        }
51
52
        return $accessUrl;
53
    }
54
}
55