Passed
Push — master ( c4afc2...9cde23 )
by Pieter van der
27:49 queued 12:42
created

VettingTypeService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 77
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A vettingTypeHint() 0 16 4
A __construct() 0 12 1
A vettingTypes() 0 18 3
1
<?php
2
3
/**
4
 * Copyright 2022 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\StepupSelfService\SelfServiceBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupMiddlewareClient\Identity\Dto\VettingTypeHint;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\NotFoundException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\VettingTypeHintService;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Value\VettingType\OnPremise;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Value\VettingType\SelfAssertedToken;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Value\VettingType\SelfVet;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Value\VettingType\VettingTypeCollection;
30
use function array_filter;
31
use function array_key_exists;
32
33
class VettingTypeService
34
{
35
    /**
36
     * @var SelfVetMarshaller
37
     */
38
    private $selfVetMarshaller;
39
40
    /**
41
     * @var SelfAssertedTokensMarshaller
42
     */
43
    private $selfAssertedTokensMarshaller;
44
45
    /**
46
     * @var ActivationFlowService
47
     */
48
    private $activationFlowService;
49
50
    /**
51
     * @var VettingTypeHintService
52
     */
53
    private $vettingTypeHintService;
54
55
    /**
56
     * @var LoggerInterface
57
     */
58
    private $logger;
59
60
    public function __construct(
61
        SelfVetMarshaller $selfVetMarshaller,
62
        SelfAssertedTokensMarshaller $selfAssertedTokensMarshaller,
63
        ActivationFlowService $activationFlowService,
64
        VettingTypeHintService $vettingTypeHintService,
65
        LoggerInterface $logger
66
    ) {
67
        $this->selfVetMarshaller = $selfVetMarshaller;
68
        $this->selfAssertedTokensMarshaller = $selfAssertedTokensMarshaller;
69
        $this->activationFlowService = $activationFlowService;
70
        $this->vettingTypeHintService = $vettingTypeHintService;
71
        $this->logger = $logger;
72
    }
73
74
    public function vettingTypes(Identity $identity, string $secondFactorId): VettingTypeCollection
75
    {
76
        $collection = new VettingTypeCollection();
77
        $this->logger->info('Adding "OnPremise" vetting type to VettingTypeCollection');
78
        $collection->add(new OnPremise());
79
        if ($this->selfAssertedTokensMarshaller->isAllowed($identity, $secondFactorId)) {
80
            $this->logger->info('Adding "SelfAssertedToken" vetting type to VettingTypeCollection');
81
            $collection->add(new SelfAssertedToken());
82
        }
83
        if ($this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) {
84
            $this->logger->info('Adding "SelfVet" vetting type to VettingTypeCollection');
85
            $collection->add(new SelfVet());
86
        }
87
        $preference = $this->activationFlowService->getPreference();
88
        $this->logger->info(sprintf('Expressing "%s" vetting type as preferred activation flow', $preference));
89
        $collection->expressVettingPreference($preference);
90
91
        return $collection;
92
    }
93
94
    public function vettingTypeHint(string $institution, string $locale): ?string
95
    {
96
        try {
97
            $hint = $this->vettingTypeHintService->findOne($institution);
98
            if (!empty($hint->hints)) {
99
                $hintText = array_filter($hint->hints, function ($hintEntry) use ($locale) {
100
                    return $hintEntry['locale'] === $locale;
101
                });
102
                if ($hintText) {
103
                    return reset($hintText)['hint'];
104
                }
105
            }
106
        } catch (NotFoundException $e) {
107
            // Do nothing for now
108
        }
109
        return null;
110
    }
111
}
112