From ac751871f9146fad0d19c6a76cbcc29e66ebd070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=A0Ja=CC=81n=20Mikla=CC=81s=CC=8C?= Date: Mon, 1 Jul 2024 13:04:32 +0200 Subject: [PATCH] Unify automation endpoint 404 exceptions [MAILPOET-5436] --- mailpoet/lib/Automation/Engine/Exceptions.php | 7 +++++++ .../Analytics/Endpoints/AutomationFlowEndpoint.php | 9 +++++---- .../MailPoet/Analytics/Endpoints/OverviewEndpoint.php | 7 ++++--- mailpoet/tasks/phpstan/phpstan-7-baseline.neon | 4 ---- mailpoet/tasks/phpstan/phpstan-8-baseline.neon | 4 ---- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index 1973fa6f0a..7a8817ae7f 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -68,6 +68,13 @@ class Exceptions { ->withMessage(sprintf(__("Automation with ID '%d' not found.", 'mailpoet'), $id)); } + public static function automationNotFoundInTimeSpan(int $id): NotFoundException { + return NotFoundException::create() + ->withErrorCode(self::AUTOMATION_NOT_FOUND) + // translators: %d is the ID of the automation. + ->withMessage(sprintf(__("Automation with ID '%d' not found in selected time span.", 'mailpoet'), $id)); + } + public static function automationVersionNotFound(int $automation, int $version): NotFoundException { return NotFoundException::create() ->withErrorCode(self::AUTOMATION_VERSION_NOT_FOUND) diff --git a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php index 57d3dc3974..b692ec3430 100644 --- a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php +++ b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/AutomationFlowEndpoint.php @@ -6,7 +6,7 @@ use MailPoet\API\REST\Request; use MailPoet\API\REST\Response; use MailPoet\Automation\Engine\API\Endpoint; use MailPoet\Automation\Engine\Data\Automation; -use MailPoet\Automation\Engine\Exceptions\NotFoundException; +use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Mappers\AutomationMapper; use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage; use MailPoet\Automation\Engine\Storage\AutomationStorage; @@ -47,14 +47,15 @@ class AutomationFlowEndpoint extends Endpoint { } public function handle(Request $request): Response { - $automation = $this->automationStorage->getAutomation(absint($request->getParam('id'))); + $id = absint($request->getParam('id')); + $automation = $this->automationStorage->getAutomation($id); if (!$automation) { - throw new NotFoundException(__('Automation not found', 'mailpoet')); + throw Exceptions::automationNotFound($id); } $query = Query::fromRequest($request); $automations = $this->automationTimeSpanController->getAutomationsInTimespan($automation, $query->getAfter(), $query->getBefore()); if (!count($automations)) { - throw new NotFoundException(__('The automation did not exist in the selected time span', 'mailpoet')); + throw Exceptions::automationNotFoundInTimeSpan($id); } $automation = current($automations); $shortStatistics = $this->automationStatisticsStorage->getAutomationStats( diff --git a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php index d898af2d12..8cf7224f9a 100644 --- a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php +++ b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php @@ -5,7 +5,7 @@ namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Endpoints; use MailPoet\API\REST\Request; use MailPoet\API\REST\Response; use MailPoet\Automation\Engine\API\Endpoint; -use MailPoet\Automation\Engine\Exceptions\NotFoundException; +use MailPoet\Automation\Engine\Exceptions; use MailPoet\Automation\Engine\Storage\AutomationStorage; use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\OverviewStatisticsController; use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare; @@ -28,9 +28,10 @@ class OverviewEndpoint extends Endpoint { } public function handle(Request $request): Response { - $automation = $this->automationStorage->getAutomation((int)$request->getParam('id')); + $id = absint($request->getParam('id')); + $automation = $this->automationStorage->getAutomation($id); if (!$automation) { - throw new NotFoundException(__('Automation not found', 'mailpoet')); + throw Exceptions::automationNotFound($id); } $query = QueryWithCompare::fromRequest($request); diff --git a/mailpoet/tasks/phpstan/phpstan-7-baseline.neon b/mailpoet/tasks/phpstan/phpstan-7-baseline.neon index 1c5f0470f2..4150a193f8 100644 --- a/mailpoet/tasks/phpstan/phpstan-7-baseline.neon +++ b/mailpoet/tasks/phpstan/phpstan-7-baseline.neon @@ -5,10 +5,6 @@ parameters: message: "#^Cannot cast mixed to string\\.$#" count: 1 path: ../../lib/Automation/Engine/Endpoints/Automations/AutomationsCreateFromTemplateEndpoint.php - - - message: "#^Cannot cast mixed to int\\.$#" - count: 1 - path: ../../lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php - message: "#^Cannot access property \\$permissions on mixed\\.$#" diff --git a/mailpoet/tasks/phpstan/phpstan-8-baseline.neon b/mailpoet/tasks/phpstan/phpstan-8-baseline.neon index 2f01a59f8e..411d2c4ce6 100644 --- a/mailpoet/tasks/phpstan/phpstan-8-baseline.neon +++ b/mailpoet/tasks/phpstan/phpstan-8-baseline.neon @@ -5,10 +5,6 @@ parameters: message: "#^Cannot cast mixed to string\\.$#" count: 1 path: ../../lib/Automation/Engine/Endpoints/Automations/AutomationsCreateFromTemplateEndpoint.php - - - message: "#^Cannot cast mixed to int\\.$#" - count: 1 - path: ../../lib/Automation/Integrations/MailPoet/Analytics/Endpoints/OverviewEndpoint.php - message: "#^Cannot access property \\$permissions on mixed\\.$#"