Passed
Pull Request — master (#5753)
by Angel Fernando Quiroz
07:27
created

AuthenticationConfigHelper::getParams()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 2
b 0
f 0
nc 10
nop 2
dl 0
loc 19
rs 9.6111
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 InvalidArgumentException;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
12
13
readonly class AuthenticationConfigHelper
14
{
15
    public function __construct(
16
        private ParameterBagInterface $parameterBag,
17
        private AccessUrlHelper $urlHelper,
18
    ) {}
19
20
    public function getParams(string $providerName, ?AccessUrl $url = null): array
21
    {
22
        $urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId();
23
24
        $authentication = $this->parameterBag->get('authentication');
25
26
        if (isset($authentication[$urlId])) {
27
            $urlParams = $authentication[$urlId];
28
        } elseif (isset($authentication['default'])) {
29
            $urlParams = $authentication['default'];
30
        } else {
31
            throw new InvalidArgumentException('Invalid access URL configuration');
32
        }
33
34
        if (!isset($urlParams[$providerName])) {
35
            throw new InvalidArgumentException('Invalid authentication provider for access URL');
36
        }
37
38
        return $urlParams[$providerName];
39
    }
40
41
    public function isEnabled(string $methodName, ?AccessUrl $url = null): bool
42
    {
43
        $configParams = $this->getParams($methodName, $url);
44
45
        return $configParams['enabled'] ?? false;
46
    }
47
}
48