1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Yproximite\Api\Service; |
5
|
|
|
|
6
|
|
|
use Yproximite\Api\Model\Location\Location; |
7
|
|
|
use Yproximite\Api\Message\Location\LocationListMessage; |
8
|
|
|
use Yproximite\Api\Message\Location\LocationPostMessage; |
9
|
|
|
use Yproximite\Api\Message\Location\LocationPatchMessage; |
10
|
|
|
use Yproximite\Api\Message\Location\LocationOverrideMessage; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LocationService |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
class LocationService extends AbstractService implements ServiceInterface |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param LocationListMessage $message |
19
|
|
|
* |
20
|
|
|
* @return Location[] |
21
|
|
|
*/ |
22
|
|
|
public function getLocations(LocationListMessage $message): array |
23
|
|
|
{ |
24
|
|
|
$path = sprintf('sites/%d/locations', $message->getSiteId()); |
25
|
|
|
|
26
|
|
|
$response = $this->getClient()->sendRequest('GET', $path); |
27
|
|
|
|
28
|
|
|
/** @var Location[] $models */ |
29
|
|
|
$models = $this->getModelFactory()->createMany(Location::class, $response); |
30
|
|
|
|
31
|
|
|
return $models; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param LocationPostMessage $message |
36
|
|
|
* |
37
|
|
|
* @return Location |
38
|
|
|
*/ |
39
|
|
|
public function postLocation(LocationPostMessage $message): Location |
40
|
|
|
{ |
41
|
|
|
$path = sprintf('sites/%d/locations', $message->getSiteId()); |
42
|
|
|
$data = ['api_location' => $message->build()]; |
43
|
|
|
|
44
|
|
|
$response = $this->getClient()->sendRequest('POST', $path, $data); |
45
|
|
|
|
46
|
|
|
/** @var Location $model */ |
47
|
|
|
$model = $this->getModelFactory()->create(Location::class, $response); |
48
|
|
|
|
49
|
|
|
return $model; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LocationPatchMessage $message |
54
|
|
|
* |
55
|
|
|
* @return Location |
56
|
|
|
*/ |
57
|
|
|
public function patchLocation(LocationPatchMessage $message): Location |
58
|
|
|
{ |
59
|
|
|
$path = sprintf('sites/%d/locations/%d', $message->getSiteId(), $message->getId()); |
60
|
|
|
$data = ['api_location' => $message->build()]; |
61
|
|
|
|
62
|
|
|
$response = $this->getClient()->sendRequest('PATCH', $path, $data); |
63
|
|
|
|
64
|
|
|
/** @var Location $model */ |
65
|
|
|
$model = $this->getModelFactory()->create(Location::class, $response); |
66
|
|
|
|
67
|
|
|
return $model; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param LocationOverrideMessage $message |
72
|
|
|
* |
73
|
|
|
* @return Location |
74
|
|
|
*/ |
75
|
|
|
public function overrideLocation(LocationOverrideMessage $message): Location |
76
|
|
|
{ |
77
|
|
|
$path = sprintf('sites/%d/locations/%d/override', $message->getSiteId(), $message->getId()); |
78
|
|
|
|
79
|
|
|
$response = $this->getClient()->sendRequest('GET', $path); |
80
|
|
|
|
81
|
|
|
/** @var Location $model */ |
82
|
|
|
$model = $this->getModelFactory()->create(Location::class, $response); |
83
|
|
|
|
84
|
|
|
return $model; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.