Passed
Push — dependabot/npm_and_yarn/microm... ( e84ba6...f2f212 )
by
unknown
10:03
created

AuthenticationConfigHelper::getParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 3
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 InvalidArgumentException;
11
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
14
use function Symfony\Component\String\u;
0 ignored issues
show
introduced by
The function Symfony\Component\String\u was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
16
readonly class AuthenticationConfigHelper
17
{
18
    public function __construct(
19
        private ParameterBagInterface $parameterBag,
20
        private AccessUrlHelper $urlHelper,
21
        private UrlGeneratorInterface $urlGenerator,
22
    ) {}
23
24
    public function getParams(string $providerName, ?AccessUrl $url = null): array
25
    {
26
        $providers = $this->getProvidersForUrl($url);
27
28
        if (!isset($providers[$providerName])) {
29
            throw new InvalidArgumentException('Invalid authentication provider for access URL');
30
        }
31
32
        return $providers[$providerName];
33
    }
34
35
    public function isEnabled(string $methodName, ?AccessUrl $url = null): bool
36
    {
37
        $configParams = $this->getParams($methodName, $url);
38
39
        return $configParams['enabled'] ?? false;
40
    }
41
42
    public function getEnabledProviders(?AccessUrl $url = null): array
43
    {
44
        $urlProviders = $this->getProvidersForUrl($url);
45
46
        $enabledProviders = [];
47
48
        foreach ($urlProviders as $providerName => $providerParams) {
49
            if ($providerParams['enabled'] ?? false) {
50
                $enabledProviders[] = [
51
                    'name' => $providerName,
52
                    'title' => $providerParams['title'] ?? u($providerName)->title(),
0 ignored issues
show
Bug introduced by
The function u was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
                    'title' => $providerParams['title'] ?? /** @scrutinizer ignore-call */ u($providerName)->title(),
Loading history...
53
                    'url' => $this->urlGenerator->generate("chamilo.oauth2_{$providerName}_start"),
54
                ];
55
            }
56
        }
57
58
        return $enabledProviders;
59
    }
60
61
    private function getProvidersForUrl(?AccessUrl $url): array
62
    {
63
        $urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId();
64
65
        $authentication = $this->parameterBag->get('authentication');
66
67
        if (isset($authentication[$urlId])) {
68
            return $authentication[$urlId];
69
        }
70
71
        if (isset($authentication['default'])) {
72
            return $authentication['default'];
73
        }
74
75
        throw new InvalidArgumentException('Invalid access URL configuration');
76
    }
77
}
78