Passed
Pull Request — master (#6532)
by Angel Fernando Quiroz
08:28
created

AccessUrlHelper::getFirstAccessUrl()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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\Helpers;
8
9
use Chamilo\CoreBundle\Entity\AccessUrl;
10
use Chamilo\CoreBundle\Framework\Container;
11
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
12
use Pdp\Rules;
13
use Pdp\TopLevelDomains;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
16
use const PHP_SAPI;
17
18
readonly class AccessUrlHelper
19
{
20
    public function __construct(
21
        private AccessUrlRepository $accessUrlRepository,
22
        private RequestStack $requestStack,
23
    ) {}
24
25
    public function isMultiple(): bool
26
    {
27
        static $accessUrlEnabled;
28
29
        if (!isset($accessUrlEnabled)) {
30
            $accessUrlEnabled = $this->accessUrlRepository->count([]) > 1;
31
        }
32
33
        return $accessUrlEnabled;
34
    }
35
36
    public function getFirstAccessUrl(): ?AccessUrl
37
    {
38
        $urlId = $this->accessUrlRepository->getFirstId();
39
40
        return $this->accessUrlRepository->find($urlId) ?: null;
41
    }
42
43
    public function getCurrent(): ?AccessUrl
44
    {
45
        static $accessUrl;
46
47
        if (!empty($accessUrl)) {
48
            return $accessUrl;
49
        }
50
51
        $accessUrl = $this->getFirstAccessUrl();
52
53
        if ('cli' === PHP_SAPI) {
54
            return $accessUrl;
55
        }
56
57
        if ($this->isMultiple()) {
58
            $request = $this->requestStack->getMainRequest();
59
60
            if (null === $request) {
61
                return $accessUrl;
62
            }
63
64
            $url = $request->getSchemeAndHttpHost().'/';
65
66
            /** @var AccessUrl $accessUrl */
67
            $accessUrl = $this->accessUrlRepository->findOneBy(['url' => $url]);
68
        }
69
70
        return $accessUrl;
71
    }
72
73
    public function isSameBaseDomain(array $urls): bool
74
    {
75
        if (empty($urls)) {
76
            return false;
77
        }
78
79
        $projectDir = Container::$container->getParameter('kernel.project_dir');
0 ignored issues
show
Bug introduced by
The method getParameter() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        /** @scrutinizer ignore-call */ 
80
        $projectDir = Container::$container->getParameter('kernel.project_dir');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
81
        $rules = Rules::fromPath($projectDir.'/config/public_suffix_list.dat');
82
83
        $firstHost = parse_url($urls[0], PHP_URL_HOST);
84
85
        if (!$firstHost) {
86
            return false;
87
        }
88
89
        $firstDomain = $rules->resolve($firstHost)->registrableDomain()->toString();
90
91
        if (!$firstDomain) {
92
            return false;
93
        }
94
95
        // Comparar con el resto
96
        foreach ($urls as $url) {
97
            $host = parse_url($url, PHP_URL_HOST);
98
99
            if (!$host) {
100
                return false;
101
            }
102
103
            $domain = $rules->resolve($host)->registrableDomain();
104
105
            if ($domain->toString() !== $firstDomain) {
106
                return false;
107
            }
108
        }
109
110
        return true;
111
    }
112
}
113