Passed
Pull Request — master (#5614)
by Angel Fernando Quiroz
11:40
created

AccessUrlHelper::getFirstAccessUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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