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