RaCandidateService::getLoa3SecondFactorTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 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\StepupRa\RaBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupBundle\Service\SecondFactorTypeService;
23
use Surfnet\StepupBundle\Value\Loa;
24
use Surfnet\StepupBundle\Value\SecondFactorType;
25
use Surfnet\StepupMiddlewareClient\Identity\Dto\RaCandidateSearchQuery;
26
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\AccreditIdentityCommand;
27
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaCandidateService as ApiRaCandidateService;
28
use Surfnet\StepupRa\RaBundle\Command\AccreditCandidateCommand;
29
use Surfnet\StepupRa\RaBundle\Command\SearchRaCandidatesCommand;
30
use Surfnet\StepupRa\RaBundle\Exception\InvalidArgumentException;
31
32
/**
33
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
34
 */
35
class RaCandidateService
36
{
37
    /**
38
     * @var \Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaCandidateService
39
     */
40
    private $apiRaCandidateService;
41
42
    /**
43
     * @var \Surfnet\StepupRa\RaBundle\Service\CommandService
44
     */
45
    private $commandService;
46
47
    /**
48
     * @var \Psr\Log\LoggerInterface
49
     */
50
    private $logger;
51
52
    /**
53
     * @var SecondFactorTypeService
54
     */
55
    private $secondFactorTypeService;
56
57
    public function __construct(
58
        ApiRaCandidateService $raCandidateService,
59
        CommandService $commandService,
60
        LoggerInterface $logger,
61
        SecondFactorTypeService $secondFactorTypeService
62
    ) {
63
        $this->apiRaCandidateService = $raCandidateService;
64
        $this->commandService = $commandService;
65
        $this->logger = $logger;
66
        $this->secondFactorTypeService = $secondFactorTypeService;
67
    }
68
69
    /**
70
     * @param SearchRaCandidatesCommand $command
71
     * @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidateCollection
72
     */
73
    public function search(SearchRaCandidatesCommand $command)
74
    {
75
        $query = new RaCandidateSearchQuery($command->actorId, $command->pageNumber);
76
77
        if ($command->name) {
78
            $query->setCommonName($command->name);
79
        }
80
81
        if ($command->email) {
82
            $query->setEmail($command->email);
83
        }
84
85
        if ($command->orderBy) {
86
            $query->setOrderBy($command->orderBy);
87
        }
88
89
        if ($command->institution) {
90
            $query->setInstitution($command->institution);
91
        }
92
93
        if ($command->raInstitution) {
94
            $query->setRaInstitution($command->raInstitution);
95
        }
96
97
        if ($command->orderDirection) {
98
            $query->setOrderDirection($command->orderDirection);
99
        }
100
101
        $query->setSecondFactorTypes($this->getLoa3SecondFactorTypes());
102
103
        return $this->apiRaCandidateService->search($query);
104
    }
105
106
    /**
107
     * @param string $identityId
108
     * @param string $actorId
109
     * @return null|\Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidateInstitutions
110
     */
111
    public function getRaCandidate($identityId, $actorId)
112
    {
113
        if (!is_string($identityId)) {
114
            throw InvalidArgumentException::invalidType('string', 'identityId', $identityId);
115
        }
116
117
        if (!is_string($actorId)) {
118
            throw InvalidArgumentException::invalidType('string', 'actorId', $actorId);
119
        }
120
121
        return $this->apiRaCandidateService->get($identityId, $actorId);
122
    }
123
124
    public function accreditCandidate(AccreditCandidateCommand $command)
125
    {
126
        $apiCommand                     = new AccreditIdentityCommand();
127
        $apiCommand->raInstitution      = $command->roleAtInstitution->getInstitution();
128
        $apiCommand->identityId         = $command->identityId;
129
        $apiCommand->institution        = $command->institution;
130
        $apiCommand->role               = $command->roleAtInstitution->getRole();
131
        $apiCommand->location           = $command->location ?: '';
132
        $apiCommand->contactInformation = $command->contactInformation ?: '';
133
134
        $result = $this->commandService->execute($apiCommand);
135
136
        if (!$result->isSuccessful()) {
137
            $this->logger->critical(
138
                sprintf(
139
                    'Accreditation of Identity "%s" of Institution "%s" for Institution "%s" with role "%s" failed: "%s"',
140
                    $apiCommand->identityId,
141
                    $apiCommand->institution,
142
                    $apiCommand->raInstitution,
143
                    $apiCommand->role,
144
                    implode(", ", $result->getErrors())
145
                )
146
            );
147
        }
148
149
        return $result->isSuccessful();
150
    }
151
152
    /**
153
     * @return string[]
154
     */
155
    private function getLoa3SecondFactorTypes()
156
    {
157
        $loa3 = new Loa(Loa::LOA_3, 'LOA3');
158
        return array_filter(
159
            $this->secondFactorTypeService->getAvailableSecondFactorTypes(),
160
            function ($secondFactorType) use ($loa3) {
161
                $secondFactorType = new SecondFactorType($secondFactorType);
162
                return $this->secondFactorTypeService->canSatisfy($secondFactorType, $loa3);
163
            }
164
        );
165
    }
166
}
167