Completed
Push — master ( 7e1619...49fbee )
by
unknown
02:24
created

RaSecondFactorService::export()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 36
rs 4.6666
c 1
b 0
f 0
cc 8
eloc 18
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
    public function revoke(RevokeSecondFactorCommand $command)
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())
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal , does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
87
            ));
88
        }
89
90
        return $result->isSuccessful();
91
    }
92
93
    /**
94
     * @param SearchRaSecondFactorsCommand $command
95
     * @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...
96
     */
97
    public function search(SearchRaSecondFactorsCommand $command)
98
    {
99
        $query = new RaSecondFactorSearchQuery($command->institution, $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->orderDirection) {
126
            $query->setOrderDirection($command->orderDirection);
127
        }
128
129
        return $this->apiRaSecondFactorService->search($query);
130
    }
131
132
    /**
133
     * Searches for a collection of second factor tokens and returns a Http response with an attachment
134
     * Content-Disposition.
135
     *
136
     * @param ExportRaSecondFactorsCommand $command
137
     * @return \Symfony\Component\HttpFoundation\Response
138
     */
139
    public function export(ExportRaSecondFactorsCommand $command)
140
    {
141
        $query = new RaSecondFactorExportQuery($command->institution);
142
143
        if ($command->name) {
144
            $query->setName($command->name);
145
        }
146
147
        if ($command->type) {
148
            $query->setType($command->type);
149
        }
150
151
        if ($command->secondFactorId) {
152
            $query->setSecondFactorId($command->secondFactorId);
153
        }
154
155
        if ($command->email) {
156
            $query->setEmail($command->email);
157
        }
158
159
        if ($command->status) {
160
            $query->setStatus($command->status);
161
        }
162
163
        if ($command->orderBy) {
164
            $query->setOrderBy($command->orderBy);
165
        }
166
167
        if ($command->orderDirection) {
168
            $query->setOrderDirection($command->orderDirection);
169
        }
170
171
        $collection = $this->apiRaSecondFactorService->searchForExport($query);
172
173
        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 171 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...
174
    }
175
}
176