Completed
Pull Request — develop (#113)
by Boy
06:28 queued 02:57
created

RaLocationService::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 20
loc 20
rs 9.4285
cc 2
eloc 14
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\StepupMiddlewareClient\Configuration\Dto\RaLocationSearchQuery;
23
use Surfnet\StepupMiddlewareClientBundle\Configuration\Command\AddRaLocationCommand as MiddlewareCreateLocationCommand;
24
use Surfnet\StepupMiddlewareClientBundle\Configuration\Command\ChangeRaLocationCommand as MiddlewareChangeRaLocationCommand;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
25
use Surfnet\StepupMiddlewareClientBundle\Configuration\Command\RemoveRaLocationCommand as MiddlewareRemoveRaLocationCommand;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
26
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\RaLocationCollection;
27
use Surfnet\StepupMiddlewareClientBundle\Configuration\Service\RaLocationService as ApiRaLocationService;
28
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid;
29
use Surfnet\StepupRa\RaBundle\Command\ChangeRaLocationCommand;
30
use Surfnet\StepupRa\RaBundle\Command\CreateRaLocationCommand;
31
use Surfnet\StepupRa\RaBundle\Command\RemoveRaLocationCommand;
32
use Surfnet\StepupRa\RaBundle\Command\RevokeSecondFactorCommand;
33
use Surfnet\StepupRa\RaBundle\Command\SearchRaLocationsCommand;
34
35
class RaLocationService
36
{
37
    /**
38
     * @param ApiRaLocationService $apiRaLocationService
39
     * @param CommandService $commandService
40
     * @param LoggerInterface $logger
41
     */
42
    public function __construct(
43
        ApiRaLocationService $apiRaLocationService,
44
        CommandService $commandService,
45
        LoggerInterface $logger
46
    ) {
47
        $this->apiRaLocationService = $apiRaLocationService;
48
        $this->commandService = $commandService;
49
        $this->logger = $logger;
50
    }
51
52
    /**
53
     * @var \Surfnet\StepupMiddlewareClientBundle\Configuration\Service\RaLocationService
54
     */
55
    private $apiRaLocationService;
56
57
    /**
58
     * @var \Surfnet\StepupRa\RaBundle\Service\CommandService
59
     */
60
    private $commandService;
61
62
    /**
63
     * @var \Psr\Log\LoggerInterface
64
     */
65
    private $logger;
66
67
    /**
68
     * @param string $id
69
     * @return null|\Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\RaLocation
70
     */
71
    public function find($id)
72
    {
73
        return $this->apiRaLocationService->get($id);
74
    }
75
76
    /**
77
     * @param SearchRaLocationsCommand $command
78
     * @return RaLocationCollection
79
     */
80
    public function search(SearchRaLocationsCommand $command)
81
    {
82
        $query = new RaLocationSearchQuery($command->institution);
83
84
        if ($command->orderBy) {
85
            $query->setOrderBy($command->orderBy);
86
        }
87
88
        if ($command->orderDirection) {
89
            $query->setOrderDirection($command->orderDirection);
90
        }
91
92
        return $this->apiRaLocationService->search($query);
93
    }
94
95 View Code Duplication
    public function create(CreateRaLocationCommand $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...
96
    {
97
        $middlewareCommand = new MiddlewareCreateLocationCommand();
98
        $middlewareCommand->id = Uuid::generate();
99
        $middlewareCommand->name = $command->name;
100
        $middlewareCommand->institution = $command->institution;
101
        $middlewareCommand->contactInformation = $command->contactInformation;
102
        $middlewareCommand->location = $command->location;
103
104
        $result = $this->commandService->execute($middlewareCommand);
105
106
        if (!$result->isSuccessful()) {
107
            $this->logger->critical(sprintf(
108
                'Creation of RA location "%s" for institution "%s" by user "%s" failed: "%s"',
109
                $middlewareCommand->name,
110
                $middlewareCommand->institution,
111
                $command->currentUserId,
112
                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...
113
            ));
114
        }
115
116
        return $result->isSuccessful();
117
    }
118
119 View Code Duplication
    public function change(ChangeRaLocationCommand $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...
120
    {
121
        $middlewareCommand = new MiddlewareChangeRaLocationCommand();
122
        $middlewareCommand->id = $command->id;
123
        $middlewareCommand->name = $command->name;
124
        $middlewareCommand->institution = $command->institution;
125
        $middlewareCommand->contactInformation = $command->contactInformation;
126
        $middlewareCommand->location = $command->location;
127
128
        $result = $this->commandService->execute($middlewareCommand);
129
130
        if (!$result->isSuccessful()) {
131
            $this->logger->critical(sprintf(
132
                'Changing of RA location "%s" for institution "%s" by user "%s" failed: "%s"',
133
                $middlewareCommand->name,
134
                $middlewareCommand->institution,
135
                $command->currentUserId,
136
                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...
137
            ));
138
        }
139
140
        return $result->isSuccessful();
141
    }
142
143
    /**
144
     * @param RemoveRaLocationCommand $command
145
     * @return bool
146
     */
147 View Code Duplication
    public function remove(RemoveRaLocationCommand $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...
148
    {
149
        $middlewareCommand = new MiddlewareRemoveRaLocationCommand();
150
        $middlewareCommand->institution = $command->institution;
151
        $middlewareCommand->authorityId = $command->currentUserId;
152
        $middlewareCommand->raLocationId = $command->locationId;
153
        $result = $this->commandService->execute($middlewareCommand);
154
155
        if (!$result->isSuccessful()) {
156
            $this->logger->critical(sprintf(
157
                'Removal of RA location "%s" of institution "%s" by user "%s" failed: "%s"',
158
                $middlewareCommand->raLocationId,
159
                $middlewareCommand->institution,
160
                $middlewareCommand->authorityId,
161
                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...
162
            ));
163
        }
164
165
        return $result->isSuccessful();
166
    }
167
}
168