Passed
Push — master ( 1e0b50...40a262 )
by Angel Fernando Quiroz
09:11
created

AccessUrlHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrent() 0 19 3
A __construct() 0 5 1
A isMultipleEnabled() 0 3 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 getCurrent(): AccessUrl
29
    {
30
        static $accessUrl;
31
32
        if (!empty($accessUrl)) {
33
            return $accessUrl;
34
        }
35
36
        $urlId = $this->accessUrlRepository->getFirstId();
37
        $accessUrl = $this->accessUrlRepository->find($urlId);
38
39
        if ($this->isMultipleEnabled()) {
40
            $url = $this->router->generate('index', [], UrlGeneratorInterface::ABSOLUTE_URL);
41
42
            /** @var AccessUrl $accessUrl */
43
            $accessUrl = $this->accessUrlRepository->findOneBy(['url' => $url]);
44
        }
45
46
        return $accessUrl;
47
    }
48
}
49