Skip to content

Commit 95ed1d8

Browse files
Thomas FinkThomas Fink
Thomas Fink
authored and
Thomas Fink
committed
fix(MPDZBS-877): Optimize next loops and schema typing
1 parent 68bfc0a commit 95ed1d8

File tree

10 files changed

+228
-243
lines changed

10 files changed

+228
-243
lines changed

zmscitizenapi/src/Zmscitizenapi/Models/Office.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
class Office extends Entity implements JsonSerializable
1010
{
11-
1211
public static $schema = 'zmsentities/schema/citizenapi/office.json';
1312

1413
/** @var int */
@@ -40,10 +39,36 @@ public function __construct(int $id, string $name, ?array $address = null, ?arra
4039
$this->id = $id;
4140
$this->name = $name;
4241
$this->address = $address;
43-
$this->geo = $geo;
42+
$this->geo = $this->validateGeo($geo);
4443
$this->scope = $scope;
4544
}
4645

46+
/**
47+
* Validate and convert geo data to an array of floats.
48+
*
49+
* @param array|null $geo
50+
* @return array|null
51+
* @throws \InvalidArgumentException If geo values are out of bounds.
52+
*/
53+
private function validateGeo(?array $geo): ?array
54+
{
55+
if (isset($geo['lat'], $geo['lon'])) {
56+
$lat = (float)$geo['lat'];
57+
$lon = (float)$geo['lon'];
58+
59+
if ($lat < -90 || $lat > 90) {
60+
throw new \InvalidArgumentException("Latitude must be between -90 and 90.");
61+
}
62+
63+
if ($lon < -180 || $lon > 180) {
64+
throw new \InvalidArgumentException("Longitude must be between -180 and 180.");
65+
}
66+
67+
return ['lat' => $lat, 'lon' => $lon];
68+
}
69+
return null;
70+
}
71+
4772
/**
4873
* Converts the model data back into an array for serialization.
4974
*

zmscitizenapi/src/Zmscitizenapi/Models/Service.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BO\Zmscitizenapi\Models;
44

5+
use BO\Zmscitizenapi\Models\Combinable;
56
use BO\Zmsentities\Schema\Entity;
67
use JsonSerializable;
78

zmscitizenapi/src/Zmscitizenapi/Services/MapperService.php

+21-13
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ class MapperService
3131
public static function mapOfficesWithScope(ProviderList $providerList): OfficeList
3232
{
3333
$offices = [];
34-
$scopes = new ScopeList(ZmsApiClientService::getScopes() ?? []);
34+
$scopes = ZmsApiClientService::getScopes() ?? new ScopeList();
3535

3636
foreach ($providerList as $provider) {
3737
$providerScope = ZmsApiFacadeService::getScopeForProvider($provider->id, $scopes);
3838

3939
$offices[] = new Office(
40-
id: (int)$provider->id,
40+
id: (int) $provider->id,
4141
name: $provider->displayName ?? $provider->name,
4242
address: $provider->data['address'] ?? null,
4343
geo: $provider->data['geo'] ?? null,
4444
scope: isset($providerScope) && !isset($providerScope['errors']) ? new ThinnedScope(
45-
id: (int)($providerScope->id ?? 0),
45+
id: (int) ($providerScope->id ?? 0),
4646
provider: $providerScope->provider ?? null,
4747
shortName: $providerScope->shortName ?? null,
4848
telephoneActivated: $providerScope->telephoneActivated ?? null,
@@ -64,18 +64,27 @@ public static function mapCombinable(array $serviceCombinations): ?Combinable
6464
return !empty($serviceCombinations) ? new Combinable($serviceCombinations) : null;
6565
}
6666

67+
/**
68+
* Map services with combinations based on request and relation lists.
69+
*
70+
* @param RequestList $requestList
71+
* @param RequestRelationList $relationList
72+
* @return ServiceList
73+
*/
6774
public static function mapServicesWithCombinations(RequestList $requestList, RequestRelationList $relationList): ServiceList
6875
{
76+
/** @var array<string, array<int>> $servicesProviderIds */
6977
$servicesProviderIds = [];
7078
foreach ($relationList as $relation) {
71-
if (!isset($servicesProviderIds[$relation->request->id])) {
72-
$servicesProviderIds[$relation->request->id] = [];
73-
}
74-
$servicesProviderIds[$relation->request->id][] = $relation->provider->id;
79+
$serviceId = $relation->request->id;
80+
$servicesProviderIds[$serviceId] ??= [];
81+
$servicesProviderIds[$serviceId][] = $relation->provider->id;
7582
}
7683

84+
/** @var Service[] $services */
7785
$services = [];
7886
foreach ($requestList as $service) {
87+
/** @var array<string, array<int>> $serviceCombinations */
7988
$serviceCombinations = [];
8089
$combinableData = $service->getAdditionalData()['combinable'] ?? [];
8190

@@ -90,7 +99,7 @@ public static function mapServicesWithCombinations(RequestList $requestList, Req
9099
$combinable = self::mapCombinable($serviceCombinations);
91100

92101
$services[] = new Service(
93-
id: (int)$service->getId(),
102+
id: (int) $service->getId(),
94103
name: $service->getName(),
95104
maxQuantity: $service->getAdditionalData()['maxQuantity'] ?? 1,
96105
combinable: $combinable ?? new Combinable()
@@ -100,14 +109,13 @@ public static function mapServicesWithCombinations(RequestList $requestList, Req
100109
return new ServiceList($services);
101110
}
102111

103-
104112
public static function mapRelations(RequestRelationList $relationList): OfficeServiceRelationList
105113
{
106114
$relations = [];
107115
foreach ($relationList as $relation) {
108116
$relations[] = new OfficeServiceRelation(
109-
officeId: (string)$relation->provider->id,
110-
serviceId: (string)$relation->request->id,
117+
officeId: (string) $relation->provider->id,
118+
serviceId: (string) $relation->request->id,
111119
slots: intval($relation->slots)
112120
);
113121
}
@@ -287,7 +295,7 @@ public static function thinnedProcessToProcess(ThinnedProcess $thinnedProcess):
287295
public static function providerToThinnedProvider(Provider $provider): ThinnedProvider
288296
{
289297
return new ThinnedProvider(
290-
id: isset($provider->id) ? (int)$provider->id : null,
298+
id: isset($provider->id) ? (int) $provider->id : null,
291299
name: $provider->name ?? null,
292300
source: $provider->source ?? null,
293301
contact: $provider->contact ?? null,
@@ -303,7 +311,7 @@ public static function providerToThinnedProvider(Provider $provider): ThinnedPro
303311
public static function thinnedProviderToProvider(ThinnedProvider $thinnedProvider): Provider
304312
{
305313
$provider = new Provider();
306-
$provider->id = isset($thinnedProvider->id) ? (string)$thinnedProvider->id : null; // Convert int ID to string
314+
$provider->id = isset($thinnedProvider->id) ? (string) $thinnedProvider->id : null; // Convert int ID to string
307315
$provider->name = $thinnedProvider->name ?? null;
308316
$provider->source = $thinnedProvider->source ?? null;
309317

0 commit comments

Comments
 (0)