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

AuthenticationConfigHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 33
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isEnabled() 0 5 1
A getParams() 0 19 5
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