SecondFactorType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translateSecondFactorType() 0 6 1
A getName() 0 3 1
A __construct() 0 4 1
A getSecondFactorTypeLogoByIdentifier() 0 21 3
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Twig\Extensions\Extension;
20
21
use Surfnet\StepupBundle\Exception\InvalidArgumentException;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeTranslationService;
23
use Surfnet\StepupBundle\Value\Provider\ViewConfigCollection;
24
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException as GatewayInvalidArgumentException;
25
use Surfnet\StepupGateway\SamlStepupProviderBundle\Provider\ViewConfig;
26
use Twig\Attribute\AsTwigFilter;
27
use Twig\Attribute\AsTwigFunction;
28
29
final class SecondFactorType
30
{
31
    private string $logoFormat = '/images/second-factor/%s.png';
32
33
    public function __construct(
34
        private readonly SecondFactorTypeTranslationService $translator,
35
        private readonly ViewConfigCollection               $collection,
36
    ) {
37
    }
38
39
    public function getName(): string
40
    {
41
        return 'ra.twig.second_factor_type';
42
    }
43
44
    #[AsTwigFilter('trans_second_factor_type')]
45
    public function translateSecondFactorType($secondFactorType): string
46
    {
47
        return $this->translator->translate(
48
            $secondFactorType,
49
            'gateway.second_factor.search.type.%s'
50
        );
51
    }
52
53
    /**
54
     * Get the logo source for a second factor type. When GSSP, the logo source
55
     * is loaded from the view config object (derived from the yml config). When
56
     * a non gssp type is encountered a source is built based on the way these
57
     * logos are typically stored in the /web/images/second-factor folder
58
     */
59
    #[AsTwigFunction('second_factor_logo')]
60
    public function getSecondFactorTypeLogoByIdentifier($secondFactorType): string
61
    {
62
        try {
63
            /** @var ViewConfig $viewConfig */
64
            $viewConfig = $this->collection->getByIdentifier($secondFactorType);
65
            $logo = $viewConfig->getLogo();
66
        } catch (InvalidArgumentException $e) {
67
            // There is no view config for this second factor type,
68
            // indicating we are dealing with a hard coded second
69
            // factor provider (like sms or yubikey)
70
            $logo = sprintf($this->logoFormat, $secondFactorType);
71
        }
72
73
        if (empty($logo)) {
74
            throw new GatewayInvalidArgumentException(
75
                "Unable to find a logo for this second factor type {$secondFactorType}"
76
            );
77
        }
78
79
        return $logo;
80
    }
81
}
82