Skip to content

Commit c582760

Browse files
committed
fix(service): Improve response validation in PageService
Refactor response validation logic to ensure entity data is correctly checked: - Consolidate checks for entity existence and structure - Update exception messages for clarity - Adjust layout, template, and site data checks to reference entity directly - Enhance test cases to reflect changes in response structure
1 parent 691a0d2 commit c582760

4 files changed

Lines changed: 137 additions & 139 deletions

File tree

src/Service/PageService.php

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,31 @@ function ($reason) {
120120
private function validateResponse(array $response): void
121121
{
122122
try {
123-
$data = $response['entity'];
124-
125-
126-
// Ensure $data is an array
127-
if (! is_array($data)) {
128-
throw new ResponseException('Response data is not an array');
123+
// Check if entity exists in the response
124+
if (! isset($response['entity']) || ! is_array($response['entity'])) {
125+
throw new ResponseException('Entity data not found in response');
129126
}
130127

128+
$entity = $response['entity'];
129+
131130
// Check if entity.page exists in the response
132-
if (! isset($data['page'])) {
131+
if (! isset($entity['page'])) {
133132
throw new ResponseException('Page data not found in response: entity.page is missing');
134133
}
135134

136-
// Check if layout exists in the response
137-
if (! isset($data['layout']) || empty($data['layout'])) {
135+
// Check if entity.layout exists in the response
136+
if (! isset($entity['layout']) || empty($entity['layout'])) {
138137
throw new ResponseException("This page don't have a layout, maybe because you're using an advanced template");
139138
}
140139

141-
// Check if template exists in the response
142-
if (! isset($data['template']) || empty($data['template'])) {
143-
throw new ResponseException('Template data not found in response: template is missing');
140+
// Check if entity.template exists in the response
141+
if (! isset($entity['template']) || empty($entity['template'])) {
142+
throw new ResponseException('Template data not found in response: entity.template is missing');
144143
}
145144

146-
// Check if site exists in the response
147-
if (! isset($data['site']) || empty($data['site'])) {
148-
throw new ResponseException('Site data not found in response: site is missing');
145+
// Check if entity.site exists in the response
146+
if (! isset($entity['site']) || empty($entity['site'])) {
147+
throw new ResponseException('Site data not found in response: entity.site is missing');
149148
}
150149
} catch (ResponseException $e) {
151150
throw $e;
@@ -168,14 +167,12 @@ private function validateResponse(array $response): void
168167
private function mapResponseToPage(array $response): Page
169168
{
170169
try {
171-
$data = $response['entity'];
172-
173-
// Ensure $data is an array and entity is an array
174-
if (! is_array($data)) {
175-
throw new ResponseException('Can\'t map response to Page: Response data is not an array');
170+
// Ensure entity is an array
171+
if (! isset($response['entity']) || ! is_array($response['entity'])) {
172+
throw new ResponseException('Can\'t map response to Page: Response entity is not an array');
176173
}
177174

178-
$pageData = $data['page'] ?? [];
175+
$pageData = $response['entity']['page'] ?? [];
179176

180177
if (! is_array($pageData)) {
181178
$pageData = [];
@@ -214,26 +211,26 @@ private function mapResponseToPage(array $response): Page
214211
private function mapResponseToPageAsset(array $response): PageAsset
215212
{
216213
try {
217-
$data = $response['entity'] ?? [];
218-
219-
// Ensure $data is an array
220-
if (! is_array($data)) {
221-
$data = [];
214+
// Ensure entity exists and is an array
215+
if (! isset($response['entity']) || ! is_array($response['entity'])) {
216+
throw new ResponseException('Entity data not found in response');
222217
}
223218

219+
$entity = $response['entity'];
220+
224221
// Extract page data
225222
$page = $this->mapResponseToPage($response);
226223

227224
// Extract layout data
228-
$layoutData = isset($data['layout']) && is_array($data['layout']) ? $data['layout'] : [];
225+
$layoutData = isset($entity['layout']) && is_array($entity['layout']) ? $entity['layout'] : [];
229226

230227
$layout = new Layout(
231228
width: $layoutData['width'] ?? null,
232229
title: $layoutData['title'] ?? '',
233-
header: $layoutData['header'] ?? true,
234-
footer: $layoutData['footer'] ?? true,
235-
body: is_array($layoutData['body'] ?? []) ? $layoutData['body'] : ['rows' => []],
236-
sidebar: is_array($layoutData['sidebar'] ?? []) ? $layoutData['sidebar'] : [
230+
header: $layoutData['header'] ?? false,
231+
footer: $layoutData['footer'] ?? false,
232+
body: $layoutData['body'] ?? ['rows' => []],
233+
sidebar: $layoutData['sidebar'] ?? [
237234
'containers' => [],
238235
'location' => '',
239236
'width' => 'small',
@@ -244,7 +241,7 @@ private function mapResponseToPageAsset(array $response): PageAsset
244241
);
245242

246243
// Extract template data
247-
$templateData = isset($data['template']) && is_array($data['template']) ? $data['template'] : [];
244+
$templateData = isset($entity['template']) && is_array($entity['template']) ? $entity['template'] : [];
248245

249246
$template = new Template(
250247
identifier: $templateData['identifier'] ?? '',
@@ -262,7 +259,7 @@ private function mapResponseToPageAsset(array $response): PageAsset
262259
);
263260

264261
// Extract site data
265-
$siteData = isset($data['site']) && is_array($data['site']) ? $data['site'] : [];
262+
$siteData = isset($entity['site']) && is_array($entity['site']) ? $entity['site'] : [];
266263

267264
$site = new Site(
268265
identifier: $siteData['identifier'] ?? '',
@@ -279,11 +276,11 @@ private function mapResponseToPageAsset(array $response): PageAsset
279276
);
280277

281278
// Extract containers
282-
$containers = isset($data['containers']) && is_array($data['containers']) ? $data['containers'] : [];
279+
$containers = isset($entity['containers']) && is_array($entity['containers']) ? $entity['containers'] : [];
283280

284281
// Extract urlContentMap if available
285282
$urlContentMap = null;
286-
$urlContentMapData = $data['urlContentMap'] ?? null;
283+
$urlContentMapData = $entity['urlContentMap'] ?? null;
287284
if (is_array($urlContentMapData)) {
288285
$urlContentMap = new Contentlet(
289286
identifier: $urlContentMapData['identifier'] ?? '',
@@ -297,18 +294,14 @@ private function mapResponseToPageAsset(array $response): PageAsset
297294
}
298295

299296
// Extract viewAs data
300-
$viewAsData = isset($data['viewAs']) && is_array($data['viewAs']) ? $data['viewAs'] : [];
297+
$viewAsData = isset($entity['viewAs']) && is_array($entity['viewAs']) ? $entity['viewAs'] : [];
301298

302-
$visitorData = $viewAsData['visitor'] ?? [];
303-
if (! is_array($visitorData)) {
304-
$visitorData = [];
305-
}
299+
$visitorData = isset($viewAsData['visitor']) && is_array($viewAsData['visitor']) ? $viewAsData['visitor'] : [];
306300

307301
// Create UserAgent
308-
$userAgentData = $visitorData['userAgent'] ?? [];
309-
if (! is_array($userAgentData)) {
310-
$userAgentData = [];
311-
}
302+
$userAgentData = isset($visitorData['userAgent']) && is_array($visitorData['userAgent'])
303+
? $visitorData['userAgent']
304+
: [];
312305

313306
$userAgent = new UserAgent(
314307
browser: $userAgentData['browser'] ?? '',
@@ -318,36 +311,33 @@ private function mapResponseToPageAsset(array $response): PageAsset
318311
);
319312

320313
// Create GeoLocation
321-
$geoData = $visitorData['geo'] ?? [];
322-
if (! is_array($geoData)) {
323-
$geoData = [];
324-
}
314+
$geoData = isset($visitorData['geo']) && is_array($visitorData['geo']) ? $visitorData['geo'] : [];
325315

326316
$geoLocation = new GeoLocation(
327317
city: $geoData['city'] ?? '',
328318
country: $geoData['country'] ?? '',
329319
countryCode: $geoData['countryCode'] ?? '',
330-
latitude: $geoData['latitude'] ?? '',
331-
longitude: $geoData['longitude'] ?? '',
320+
latitude: (float)($geoData['latitude'] ?? 0),
321+
longitude: (float)($geoData['longitude'] ?? 0),
332322
region: $geoData['region'] ?? ''
333323
);
334324

335325
// Create Visitor
336326
$visitor = new Visitor(
337-
tags: is_array($visitorData['tags'] ?? []) ? $visitorData['tags'] : [],
327+
tags: [],
338328
device: $visitorData['device'] ?? '',
339329
isNew: $visitorData['isNew'] ?? false,
340330
userAgent: $userAgent,
341331
referer: $visitorData['referer'] ?? '',
342332
dmid: $visitorData['dmid'] ?? '',
343333
geo: $geoLocation,
344-
personas: is_array($visitorData['personas'] ?? []) ? $visitorData['personas'] : []
334+
personas: []
345335
);
346336

347337
// Create ViewAs
348338
$viewAs = new ViewAs(
349339
visitor: $visitor,
350-
language: is_array($viewAsData['language'] ?? []) ? $viewAsData['language'] : [],
340+
language: [],
351341
mode: $viewAsData['mode'] ?? 'LIVE'
352342
);
353343

tests/Model/PageAssetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private function createPageAsset(): PageAsset
2828
new UserAgent('Chrome', '120.0', 'Windows', false), // userAgent
2929
'https://example.com', // referer
3030
'test-dmid', // dmid
31-
new GeoLocation('US', 'United States', 'FL', 'Florida', 'Miami', '33101', 25.7743, -80.1937), // geo
31+
new GeoLocation('Miami', 'United States', 'US', 25.7743, -80.1937, 'Florida'), // geo
3232
[] // personas
3333
);
3434

@@ -93,7 +93,7 @@ public function testIsGenerated(): void
9393
new UserAgent('Chrome', '120.0', 'Windows', false), // userAgent
9494
'https://example.com', // referer
9595
'test-dmid', // dmid
96-
new GeoLocation('US', 'United States', 'FL', 'Florida', 'Miami', '33101', 25.7743, -80.1937), // geo
96+
new GeoLocation('Miami', 'United States', 'US', 25.7743, -80.1937, 'Florida'), // geo
9797
[] // personas
9898
);
9999

tests/Service/PageServiceTest.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,12 @@ public function testGetPageWithEmptyPageData(): void
232232
200,
233233
['Content-Type' => 'application/json'],
234234
json_encode([
235-
'entity' => ['page' => []],
236-
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
237-
'template' => ['identifier' => 'test-template'],
238-
'site' => ['identifier' => 'test-site'],
235+
'entity' => [
236+
'page' => [],
237+
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
238+
'template' => ['identifier' => 'test-template'],
239+
'site' => ['identifier' => 'test-site'],
240+
],
239241
])
240242
)
241243
);
@@ -255,9 +257,11 @@ public function testGetPageWithMissingLayout(): void
255257
200,
256258
['Content-Type' => 'application/json'],
257259
json_encode([
258-
'entity' => ['page' => ['identifier' => 'test-page']],
259-
'template' => ['identifier' => 'test-template'],
260-
'site' => ['identifier' => 'test-site'],
260+
'entity' => [
261+
'page' => ['identifier' => 'test-page'],
262+
'template' => ['identifier' => 'test-template'],
263+
'site' => ['identifier' => 'test-site'],
264+
],
261265
])
262266
)
263267
);
@@ -277,17 +281,19 @@ public function testGetPageWithMissingTemplate(): void
277281
200,
278282
['Content-Type' => 'application/json'],
279283
json_encode([
280-
'entity' => ['page' => ['identifier' => 'test-page']],
281-
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
282-
'site' => ['identifier' => 'test-site'],
284+
'entity' => [
285+
'page' => ['identifier' => 'test-page'],
286+
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
287+
'site' => ['identifier' => 'test-site'],
288+
],
283289
])
284290
)
285291
);
286292

287293
$request = new PageRequest('/about-us');
288294

289295
$this->expectException(ResponseException::class);
290-
$this->expectExceptionMessage('Template data not found in response: template is missing');
296+
$this->expectExceptionMessage('Template data not found in response: entity.template is missing');
291297

292298
$this->pageService->getPage($request);
293299
}
@@ -299,17 +305,19 @@ public function testGetPageWithMissingSite(): void
299305
200,
300306
['Content-Type' => 'application/json'],
301307
json_encode([
302-
'entity' => ['page' => ['identifier' => 'test-page']],
303-
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
304-
'template' => ['identifier' => 'test-template'],
308+
'entity' => [
309+
'page' => ['identifier' => 'test-page'],
310+
'layout' => ['title' => 'Test Layout', 'body' => ['rows' => []]],
311+
'template' => ['identifier' => 'test-template'],
312+
],
305313
])
306314
)
307315
);
308316

309317
$request = new PageRequest('/about-us');
310318

311319
$this->expectException(ResponseException::class);
312-
$this->expectExceptionMessage('Site data not found in response: site is missing');
320+
$this->expectExceptionMessage('Site data not found in response: entity.site is missing');
313321

314322
$this->pageService->getPage($request);
315323
}

0 commit comments

Comments
 (0)