Completed
Push — develop ( 784583...9a84aa )
by Michiel
02:05 queued 10s
created

RaCandidateService::getRaCandidateByIdentityId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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->actorInstitution, $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->orderDirection) {
94
            $query->setOrderDirection($command->orderDirection);
95
        }
96
97
        $query->setSecondFactorTypes($this->getLoa3SecondFactorTypes());
98
99
        return $this->apiRaCandidateService->search($query);
100
    }
101
102
    /**
103
     * @param string $identityId
104
     * @param string $institution
105
     * @return null|\Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidate
106
     */
107
    public function getRaCandidate($identityId, $institution)
108
    {
109
        if (!is_string($identityId)) {
110
            throw InvalidArgumentException::invalidType('string', 'identityId', $identityId);
111
        }
112
113
        return $this->apiRaCandidateService->get($identityId, $institution);
114
    }
115
116
    public function accreditCandidate(AccreditCandidateCommand $command)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
117
    {
118
        $apiCommand                     = new AccreditIdentityCommand();
119
        $apiCommand->raInstitution      = $command->raInstitution;
120
        $apiCommand->identityId         = $command->identityId;
121
        $apiCommand->institution        = $command->institution;
122
        $apiCommand->role               = $command->role;
123
        $apiCommand->location           = $command->location ?: '';
124
        $apiCommand->contactInformation = $command->contactInformation ?: '';
125
126
        $result = $this->commandService->execute($apiCommand);
127
128
        if (!$result->isSuccessful()) {
129
            $this->logger->critical(
130
                sprintf(
131
                    'Accreditation of Identity "%s" of Institution "%s" with role "%s" failed: "%s"',
132
                    $apiCommand->identityId,
133
                    $apiCommand->institution,
134
                    $apiCommand->role,
135
                    implode(", ", $result->getErrors())
136
                )
137
            );
138
        }
139
140
        return $result->isSuccessful();
141
    }
142
143
    /**
144
     * @return string[]
145
     */
146
    private function getLoa3SecondFactorTypes()
147
    {
148
        $loa3 = new Loa(Loa::LOA_3, 'LOA3');
149
        return array_filter(
150
            $this->secondFactorTypeService->getAvailableSecondFactorTypes(),
151
            function ($secondFactorType) use ($loa3) {
152
                $secondFactorType = new SecondFactorType($secondFactorType);
153
                return $this->secondFactorTypeService->canSatisfy($secondFactorType, $loa3);
154
            }
155
        );
156
    }
157
}
158