Completed
Push — feature/fga-vetting-procedure ( 40f6c5...a96684 )
by Michiel
11:42
created

RaSecondFactorService::export()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 36

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
dl 36
loc 36
c 0
b 0
f 0
rs 7.9128
cc 8
nc 128
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\StepupMiddlewareClient\Identity\Dto\RaSecondFactorExportQuery;
23
use Surfnet\StepupMiddlewareClient\Identity\Dto\RaSecondFactorSearchQuery;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\RevokeRegistrantsSecondFactorCommand;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaSecondFactorCollection;
26
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaSecondFactorService as ApiRaSecondFactorService;
27
use Surfnet\StepupRa\RaBundle\Command\ExportRaSecondFactorsCommand;
28
use Surfnet\StepupRa\RaBundle\Command\RevokeSecondFactorCommand;
29
use Surfnet\StepupRa\RaBundle\Command\SearchRaSecondFactorsCommand;
30
31
class RaSecondFactorService
32
{
33
    /**
34
     * @var \Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaSecondFactorService
35
     */
36
    private $apiRaSecondFactorService;
37
38
    /**
39
     * @var \Surfnet\StepupRa\RaBundle\Service\CommandService
40
     */
41
    private $commandService;
42
43
    /**
44
     * @var RaSecondFactorExport
45
     */
46
    private $exporter;
47
48
    /**
49
     * @var \Psr\Log\LoggerInterface
50
     */
51
    private $logger;
52
53
    /**
54
     * @param ApiRaSecondFactorService $apiRaSecondFactorService
55
     * @param CommandService $commandService
56
     * @param RaSecondFactorExport $exporter
57
     * @param LoggerInterface $logger
58
     */
59
    public function __construct(
60
        ApiRaSecondFactorService $apiRaSecondFactorService,
61
        CommandService $commandService,
62
        RaSecondFactorExport $exporter,
63
        LoggerInterface $logger
64
    ) {
65
        $this->apiRaSecondFactorService = $apiRaSecondFactorService;
66
        $this->commandService = $commandService;
67
        $this->exporter = $exporter;
68
        $this->logger = $logger;
69
    }
70
71 View Code Duplication
    public function revoke(RevokeSecondFactorCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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...
72
    {
73
        $middlewareCommand                 = new RevokeRegistrantsSecondFactorCommand();
74
        $middlewareCommand->secondFactorId = $command->secondFactorId;
75
        $middlewareCommand->identityId     = $command->identityId;
76
        $middlewareCommand->authorityId    = $command->currentUserId;
77
78
        $result = $this->commandService->execute($middlewareCommand);
79
80
        if (!$result->isSuccessful()) {
81
            $this->logger->critical(sprintf(
82
                'Revocation of Second Factor "%s" of Identity "%s" by user "%s" failed: "%s"',
83
                $middlewareCommand->secondFactorId,
84
                $middlewareCommand->identityId,
85
                $middlewareCommand->authorityId,
86
                implode(", ", $result->getErrors())
87
            ));
88
        }
89
90
        return $result->isSuccessful();
91
    }
92
93
    /**
94
     * @param SearchRaSecondFactorsCommand $command
95
     * @return RaSecondFactorCollection
96
     */
97 View Code Duplication
    public function search(SearchRaSecondFactorsCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $query = new RaSecondFactorSearchQuery($command->actorInstitution, $command->pageNumber);
100
101
        if ($command->name) {
102
            $query->setName($command->name);
103
        }
104
105
        if ($command->type) {
106
            $query->setType($command->type);
107
        }
108
109
        if ($command->secondFactorId) {
110
            $query->setSecondFactorId($command->secondFactorId);
111
        }
112
113
        if ($command->email) {
114
            $query->setEmail($command->email);
115
        }
116
117
        if ($command->status) {
118
            $query->setStatus($command->status);
119
        }
120
121
        if ($command->orderBy) {
122
            $query->setOrderBy($command->orderBy);
123
        }
124
        
125
        if ($command->institution) {
126
            $query->setInstitution($command->institution);
127
        }
128
129
        if ($command->orderDirection) {
130
            $query->setOrderDirection($command->orderDirection);
131
        }
132
133
        return $this->apiRaSecondFactorService->search($query);
134
    }
135
136
    /**
137
     * Searches for a collection of second factor tokens and returns a Http response with an attachment
138
     * Content-Disposition.
139
     *
140
     * @param ExportRaSecondFactorsCommand $command
141
     * @return \Symfony\Component\HttpFoundation\Response
142
     */
143 View Code Duplication
    public function export(ExportRaSecondFactorsCommand $command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        $query = new RaSecondFactorExportQuery($command->institution);
146
147
        if ($command->name) {
148
            $query->setName($command->name);
149
        }
150
151
        if ($command->type) {
152
            $query->setType($command->type);
153
        }
154
155
        if ($command->secondFactorId) {
156
            $query->setSecondFactorId($command->secondFactorId);
157
        }
158
159
        if ($command->email) {
160
            $query->setEmail($command->email);
161
        }
162
163
        if ($command->status) {
164
            $query->setStatus($command->status);
165
        }
166
167
        if ($command->orderBy) {
168
            $query->setOrderBy($command->orderBy);
169
        }
170
171
        if ($command->orderDirection) {
172
            $query->setOrderDirection($command->orderDirection);
173
        }
174
175
        $collection = $this->apiRaSecondFactorService->searchForExport($query);
176
177
        return $this->exporter->export($collection, $query->getFileName());
178
    }
179
}
180