Completed
Push — main ( d02cfb...38ec3a )
by
unknown
22s queued 15s
created

SecondFactorService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoaLevel() 0 9 3
A __construct() 0 10 1
A findByUuid() 0 7 2
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\Service\SecondFactor\SecondFactorInterface;
28
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\Gateway\GsspFallbackService;
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
    private GsspFallbackService $gsspFallbackService;
37
38
    public function __construct(
39
        SecondFactorRepository  $repository,
40
        LoaResolutionService    $loaResolutionService,
41
        SecondFactorTypeService $secondFactorTypeService,
42
        GsspFallbackService     $gsspFallbackService
43
    ) {
44
        $this->repository = $repository;
45
        $this->loaResolutionService = $loaResolutionService;
46
        $this->secondFactorTypeService = $secondFactorTypeService;
47
        $this->gsspFallbackService = $gsspFallbackService;
48
    }
49
50
    /**
51
     * @param $uuid
52
     * @return null|SecondFactorInterface
53
     */
54
    public function findByUuid($uuid)
55
    {
56
        if ($this->gsspFallbackService->isSecondFactorFallback()) {
57
            return $this->gsspFallbackService->createSecondFactor();
58
        }
59
60
        return $this->repository->findOneBySecondFactorId($uuid);
61
    }
62
63
    public function getLoaLevel(SecondFactorInterface $secondFactor): Loa
64
    {
65
        if ($secondFactor instanceof SecondFactor) {
66
            return $this->loaResolutionService->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService));
67
        } elseif ($secondFactor instanceof SecondfactorGsspFallback) {
68
            return $this->loaResolutionService->getLoaByLevel(Loa::LOA_SELF_VETTED);
69
        }
70
71
        throw new RuntimeException('Unknown second factor type to determine Loa level');
72
    }
73
}
74