Completed
Push — develop ( eb590e...2ea15c )
by Boy
9s
created

RaLocationService::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 23
Ratio 100 %

Importance

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