Passed
Push — feature/add-azure-mfa-registra... ( 3767e0...d31ca4 )
by
unknown
02:11
created

SecondFactorService::getLoaLevel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\Service;
20
21
use Surfnet\StepupBundle\Service\LoaResolutionService;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
23
use Surfnet\StepupBundle\Value\Loa;
24
use Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor;
25
use Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactorRepository;
26
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException;
27
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext;
28
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactor\SecondFactorInterface;
29
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\Gateway\SecondfactorGsspFallback;
30
31
class SecondFactorService
32
{
33
    private SecondFactorRepository $repository;
34
    private LoaResolutionService $loaResolutionService;
35
    private SecondFactorTypeService $secondFactorTypeService;
36
37
    public function __construct(
38
        SecondFactorRepository  $repository,
39
        LoaResolutionService    $loaResolutionService,
40
        SecondFactorTypeService $secondFactorTypeService
41
    ) {
42
        $this->repository = $repository;
43
        $this->loaResolutionService = $loaResolutionService;
44
        $this->secondFactorTypeService = $secondFactorTypeService;
45
    }
46
47
    /**
48
     * @param $uuid
49
     * @return null|SecondFactorInterface
50
     */
51
    public function findByUuid($uuid, ResponseContext $responseContext)
52
    {
53
        if ($responseContext->isSecondFactorFallback()) {
54
            return SecondfactorGsspFallback::create('azuremfa', $responseContext->getSelectedLocale());
55
        }
56
57
        return $this->repository->findOneBySecondFactorId($uuid);
58
    }
59
60
    public function getLoaLevel(SecondFactorInterface $secondFactor): Loa
61
    {
62
        if ($secondFactor instanceof SecondFactor) {
63
            return $this->loaResolutionService->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService));
64
        } elseif ($secondFactor instanceof SecondfactorGsspFallback) {
65
            return $this->loaResolutionService->getLoaByLevel(Loa::LOA_SELF_VETTED);
66
        }
67
68
        throw new RuntimeException('Unknown second factor type to determine Loa level');
69
    }
70
}
71