Unify automation endpoint 404 exceptions

[MAILPOET-5436]
This commit is contained in:
 Ján Mikláš
2024-07-01 13:04:32 +02:00
committed by Aschepikov
parent 8376494d6f
commit ac751871f9
5 changed files with 16 additions and 15 deletions

View File

@@ -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)

View File

@@ -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(

View File

@@ -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);

View File

@@ -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\\.$#"

View File

@@ -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\\.$#"