RaLocationService::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
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;
25
use Surfnet\StepupMiddlewareClientBundle\Configuration\Command\RemoveRaLocationCommand as MiddlewareRemoveRaLocationCommand;
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())
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())
134
            ));
135
        }
136
137
        return $result->isSuccessful();
138
    }
139
140
    /**
141
     * @param RemoveRaLocationCommand $command
142
     * @return bool
143
     */
144
    public function remove(RemoveRaLocationCommand $command)
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())
158
            ));
159
        }
160
161
        return $result->isSuccessful();
162
    }
163
}
164