Skip to content

Commit 8ba4b9f

Browse files
committed
fix national calendar PATCH update
1 parent 1c7581d commit 8ba4b9f

File tree

1 file changed

+91
-83
lines changed

1 file changed

+91
-83
lines changed

src/Paths/RegionalData.php

+91-83
Original file line numberDiff line numberDiff line change
@@ -400,70 +400,68 @@ private function createRegionalCalendar(): void
400400
*
401401
* It is private as it is called from {@see updateRegionalCalendar}.
402402
*
403-
* The resource is created or updated in the `jsondata/sourcedata/nations/` directory.
404-
*
405-
* If the payload is valid according to {@see LitSchema::NATIONAL}, the response will be a JSON object
406-
* containing a success message.
407-
*
408-
* If the payload is invalid, the response will be a JSON error response with a 422 status code.
403+
* If the resource to update is not found in the national calendars index, the response will be a JSON error response with a status code of 404 Not Found.
404+
* If the resource to update is not writable or the write was not successful, the response will be a JSON error response with a status code of 503 Service Unavailable.
405+
* The resource is updated in the `jsondata/sourcedata/calendars/nations/` directory.
409406
*/
410407
private function handleNationalCalendarUpdate()
411408
{
412-
$response = new \stdClass();
409+
$nationEntry = array_values(array_filter($this->CalendarsMetadata->national_calendars, function ($item) {
410+
return $item->calendar_id === $this->params->key;
411+
}));
412+
413+
if ( empty($nationEntry) ) {
414+
self::produceErrorResponse(StatusCode::NOT_FOUND, "Cannot update unknown national calendar resource {$this->params->key}.");
415+
}
416+
417+
foreach ($this->params->payload->i18n as $locale => $i18nData) {
418+
$calendarI18nFile = strtr(
419+
JsonData::NATIONAL_CALENDARS_I18N_FILE,
420+
[
421+
'{nation}' => $this->params->key,
422+
'{locale}' => $locale
423+
]
424+
);
425+
426+
if (false === is_writable($calendarI18nFile)) {
427+
self::produceErrorResponse(StatusCode::SERVICE_UNAVAILABLE, "Cannot update national calendar i18n resource for {$this->params->key} at {$calendarI18nFile}, check file and folder permissions.");
428+
}
429+
430+
// Update national calendar i18n data for locale
431+
$calendarI18nData = json_encode($i18nData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
432+
if (
433+
false === file_put_contents(
434+
$calendarI18nFile,
435+
$calendarI18nData . PHP_EOL
436+
)
437+
) {
438+
self::produceErrorResponse(StatusCode::SERVICE_UNAVAILABLE, "Could not update national calendar i18n resource {$this->params->key} in path {$calendarI18nFile}.");
439+
}
440+
}
441+
442+
unset($this->params->payload->i18n);
443+
413444
$calendarFile = strtr(
414445
JsonData::NATIONAL_CALENDARS_FILE,
415446
[
416447
'{nation}' => $this->params->key
417448
]
418449
);
419450

420-
$calendarI18nFile = strtr(
421-
JsonData::NATIONAL_CALENDARS_I18N_FILE,
422-
[
423-
'{nation}' => $this->params->key,
424-
'{locale}' => $this->params->locale
425-
]
426-
);
451+
$calendarData = json_encode($this->params->payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
427452

428-
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::NATIONAL);
429-
if ($test === true) {
430-
// We need to extract localized name properties from the payload,
431-
// and write them to the corresponding locale file,
432-
// but only for createNew or setProperty::name actions,
433-
// or for the makePatron action.
434-
// Make sure &$value is a reference, not a copy, so that the `name` property will be deleted on the original object.
435-
$i18nPayload = [];
436-
foreach ($this->params->payload->litcal as &$value) {
437-
if (
438-
$value->metadata->action === 'createNew'
439-
|| ($value->metadata->action === 'setProperty' && $value->metadata->property === 'name')
440-
|| $value->metadata->action === 'makePatron'
441-
) {
442-
$i18nPayload[$value->festivity->event_key] = $value->festivity->name;
443-
}
444-
unset($value->festivity->name);
445-
}
446-
$data = json_encode(
447-
$this->params->payload,
448-
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
449-
);
450-
$i18nData = json_encode(
451-
$i18nPayload,
452-
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
453-
);
454-
file_put_contents(
453+
if (
454+
false === file_put_contents(
455455
$calendarFile,
456-
$data . PHP_EOL
457-
);
458-
file_put_contents(
459-
$calendarI18nFile,
460-
$i18nData . PHP_EOL
461-
);
462-
$response->success = "Calendar data created or updated for Nation \"{$this->params->key}\"";
463-
self::produceResponse(json_encode($response));
464-
} else {
465-
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
456+
$calendarData . PHP_EOL
457+
)
458+
) {
459+
self::produceErrorResponse(StatusCode::SERVICE_UNAVAILABLE, "Could not update national calendar resource {$this->params->key} in path {$calendarFile}.");
466460
}
461+
462+
$response = new \stdClass();
463+
$response->success = "Calendar data created or updated for Nation \"{$this->params->key}\"";
464+
self::produceResponse(json_encode($response));
467465
}
468466

469467
/**
@@ -512,37 +510,30 @@ private function handleWiderRegionCalendarUpdate()
512510
}
513511
}
514512

515-
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::WIDERREGION);
516-
if ($test === true) {
517-
$data = json_encode($this->params->payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
518-
$widerRegionFile = strtr(
519-
JsonData::WIDER_REGIONS_FILE,
520-
[
521-
'{wider_region}' => $widerRegion
522-
]
523-
);
524-
file_put_contents($widerRegionFile, $data . PHP_EOL);
525-
$response->success = "Calendar data created or updated for Wider Region \"{$widerRegion}\"";
526-
self::produceResponse(json_encode($response));
527-
} else {
528-
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
529-
}
513+
$data = json_encode($this->params->payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
514+
$widerRegionFile = strtr(
515+
JsonData::WIDER_REGIONS_FILE,
516+
[
517+
'{wider_region}' => $widerRegion
518+
]
519+
);
520+
file_put_contents($widerRegionFile, $data . PHP_EOL);
521+
$response->success = "Calendar data created or updated for Wider Region \"{$widerRegion}\"";
522+
self::produceResponse(json_encode($response));
530523
}
531524

532525
/**
533526
* Handle PATCH requests to create or update a diocesan calendar data resource.
534527
*
535528
* It is private as it is called from {@see updateRegionalCalendar}.
536529
*
537-
* The resource is created or updated in the `jsondata/sourcedata/nations/` directory.
530+
* The resource is created or updated in the `jsondata/sourcedata/calendars/dioceses/` directory.
538531
*
539532
* If the payload is valid according to {@see LitSchema::DIOCESAN}, the response will be a JSON object
540533
* containing a success message.
541534
*
542-
* If the resource to update is not found in the diocesan calendars index or in the `jsondata/sourcedata/nations/{nation}/` directory, the response will be a JSON error response with a status code of 404 Not Found.
543-
* If the payload is not an object, the response will be a JSON error response with a status code of 400 Bad Request.
535+
* If the resource to update is not found in the diocesan calendars index, the response will be a JSON error response with a status code of 404 Not Found.
544536
* If the resource to update is not writable or the write was not successful, the response will be a JSON error response with a status code of 503 Service Unavailable.
545-
* If the payload is not valid according to {@see LitSchema::DIOCESAN}, the response will be a JSON error response with a status code of 422 Unprocessable Content.
546537
*
547538
*/
548539
private function handleDiocesanCalendarUpdate()
@@ -552,13 +543,7 @@ private function handleDiocesanCalendarUpdate()
552543
}));
553544

554545
if ( empty($dioceseEntry) ) {
555-
self::produceErrorResponse(StatusCode::NOT_FOUND, "Cannot update diocesan calendar resource {$this->params->key}.");
556-
}
557-
558-
// Validate payload against schema
559-
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::DIOCESAN);
560-
if (false === $test) {
561-
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
546+
self::produceErrorResponse(StatusCode::NOT_FOUND, "Cannot update unknown diocesan calendar resource {$this->params->key}.");
562547
}
563548

564549
foreach ($this->params->payload->i18n as $locale => $i18nData) {
@@ -637,15 +622,38 @@ private function handleDiocesanCalendarUpdate()
637622
*/
638623
private function updateRegionalCalendar()
639624
{
625+
if (false === $this->params->payload instanceof \stdClass) {
626+
$payloadType = gettype($this->params->payload);
627+
self::produceErrorResponse(StatusCode::BAD_REQUEST, "`payload` param expected to be serialized object, instead it was of type `{$payloadType}` after unserialization");
628+
}
629+
640630
switch ($this->params->category) {
641-
case 'NATIONALCALENDAR':
642-
$this->handleNationalCalendarUpdate();
631+
case "DIOCESANCALENDAR":
632+
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::DIOCESAN);
633+
if (true === $test) {
634+
$this->handleDiocesanCalendarUpdate();
635+
} else {
636+
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
637+
}
638+
break;
639+
case "NATIONALCALENDAR":
640+
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::NATIONAL);
641+
if (true === $test) {
642+
$this->handleNationalCalendarUpdate();
643+
} else {
644+
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
645+
}
643646
break;
644-
case 'WIDERREGIONCALENDAR':
645-
$this->handleWiderRegionCalendarUpdate();
647+
case "WIDERREGIONCALENDAR":
648+
$test = $this->validateDataAgainstSchema($this->params->payload, LitSchema::WIDERREGION);
649+
if (true === $test) {
650+
$this->handleWiderRegionCalendarUpdate();
651+
} else {
652+
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, $test);
653+
}
646654
break;
647-
case 'DIOCESANCALENDAR':
648-
$this->handleDiocesanCalendarUpdate();
655+
default:
656+
self::produceErrorResponse(StatusCode::UNPROCESSABLE_CONTENT, "Unknown calendar category \"{$this->params->category}\"");
649657
}
650658
}
651659

0 commit comments

Comments
 (0)