RaSecondFactorService::export()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 6.4977
c 0
b 0
f 0
cc 9
nc 256
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
/**
32
 * @SuppressWarnings(PHPMD.NPathComplexity) -- The command to query mapping in search and export exceed the
33
 * NPathComplexity threshold.
34
 */
35
class RaSecondFactorService
36
{
37
    /**
38
     * @var \Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaSecondFactorService
39
     */
40
    private $apiRaSecondFactorService;
41
42
    /**
43
     * @var \Surfnet\StepupRa\RaBundle\Service\CommandService
44
     */
45
    private $commandService;
46
47
    /**
48
     * @var RaSecondFactorExport
49
     */
50
    private $exporter;
51
52
    /**
53
     * @var \Psr\Log\LoggerInterface
54
     */
55
    private $logger;
56
57
    /**
58
     * @param ApiRaSecondFactorService $apiRaSecondFactorService
59
     * @param CommandService $commandService
60
     * @param RaSecondFactorExport $exporter
61
     * @param LoggerInterface $logger
62
     */
63
    public function __construct(
64
        ApiRaSecondFactorService $apiRaSecondFactorService,
65
        CommandService $commandService,
66
        RaSecondFactorExport $exporter,
67
        LoggerInterface $logger
68
    ) {
69
        $this->apiRaSecondFactorService = $apiRaSecondFactorService;
70
        $this->commandService = $commandService;
71
        $this->exporter = $exporter;
72
        $this->logger = $logger;
73
    }
74
75 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...
76
    {
77
        $middlewareCommand                 = new RevokeRegistrantsSecondFactorCommand();
78
        $middlewareCommand->secondFactorId = $command->secondFactorId;
79
        $middlewareCommand->identityId     = $command->identityId;
80
        $middlewareCommand->authorityId    = $command->currentUserId;
81
82
        $result = $this->commandService->execute($middlewareCommand);
83
84
        if (!$result->isSuccessful()) {
85
            $this->logger->critical(sprintf(
86
                'Revocation of Second Factor "%s" of Identity "%s" by user "%s" failed: "%s"',
87
                $middlewareCommand->secondFactorId,
88
                $middlewareCommand->identityId,
89
                $middlewareCommand->authorityId,
90
                implode(", ", $result->getErrors())
91
            ));
92
        }
93
94
        return $result->isSuccessful();
95
    }
96
97
    /**
98
     * @param SearchRaSecondFactorsCommand $command
99
     * @return RaSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be RaSecondFactorCollection|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
100
     */
101
    public function search(SearchRaSecondFactorsCommand $command)
102
    {
103
        $query = new RaSecondFactorSearchQuery($command->pageNumber, $command->actorId);
104
105
        if ($command->name) {
106
            $query->setName($command->name);
107
        }
108
109
        if ($command->type) {
110
            $query->setType($command->type);
111
        }
112
113
        if ($command->secondFactorId) {
114
            $query->setSecondFactorId($command->secondFactorId);
115
        }
116
117
        if ($command->email) {
118
            $query->setEmail($command->email);
119
        }
120
121
        if ($command->institution) {
122
            $query->setInstitution($command->institution);
123
        }
124
125
        if ($command->status) {
126
            $query->setStatus($command->status);
127
        }
128
129
        if ($command->orderBy) {
130
            $query->setOrderBy($command->orderBy);
131
        }
132
133
        if ($command->orderDirection) {
134
            $query->setOrderDirection($command->orderDirection);
135
        }
136
137
        return $this->apiRaSecondFactorService->search($query);
138
    }
139
140
    /**
141
     * Searches for a collection of second factor tokens and returns a Http response with an attachment
142
     * Content-Disposition.
143
     *
144
     * @param ExportRaSecondFactorsCommand $command
145
     * @return \Symfony\Component\HttpFoundation\Response
146
     */
147
    public function export(ExportRaSecondFactorsCommand $command)
148
    {
149
        $query = new RaSecondFactorExportQuery($command->actorId);
150
151
        if ($command->name) {
152
            $query->setName($command->name);
153
        }
154
155
        if ($command->type) {
156
            $query->setType($command->type);
157
        }
158
159
        if ($command->secondFactorId) {
160
            $query->setSecondFactorId($command->secondFactorId);
161
        }
162
163
        if ($command->email) {
164
            $query->setEmail($command->email);
165
        }
166
167
        if ($command->institution) {
168
            $query->setInstitution($command->institution);
169
        }
170
171
        if ($command->status) {
172
            $query->setStatus($command->status);
173
        }
174
175
        if ($command->orderBy) {
176
            $query->setOrderBy($command->orderBy);
177
        }
178
179
        if ($command->orderDirection) {
180
            $query->setOrderDirection($command->orderDirection);
181
        }
182
183
        $collection = $this->apiRaSecondFactorService->searchForExport($query);
184
185
        return $this->exporter->export($collection, $query->getFileName());
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $this->apiRaSecondFactor...searchForExport($query) on line 183 can be null; however, Surfnet\StepupRa\RaBundl...dFactorExport::export() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
186
    }
187
}
188