Refactor duplicate segment name checks

This creates a single method we can rely on to check for the uniqueness
of a segment name and refactors places where we had duplicate code to
use the new method.

[MAILPOET-3998]
This commit is contained in:
John Oleksowicz
2021-12-20 12:24:09 -06:00
committed by Veljko V
parent c23d8cae53
commit df119d18a0
8 changed files with 82 additions and 30 deletions

View File

@@ -3,12 +3,12 @@
namespace MailPoet\API\JSON\v1;
use Exception;
use InvalidArgumentException;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error as APIError;
use MailPoet\API\JSON\Response;
use MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder;
use MailPoet\Config\AccessControl;
use MailPoet\ConflictException;
use MailPoet\Cron\CronWorkerScheduler;
use MailPoet\Cron\Workers\WooCommerceSync;
use MailPoet\Doctrine\Validator\ValidationException;
@@ -125,7 +125,7 @@ class Segments extends APIEndpoint {
return $this->badRequest([
APIError::BAD_REQUEST => __('Please specify a name.', 'mailpoet'),
]);
} catch (InvalidArgumentException $exception) {
} catch (ConflictException $exception) {
return $this->badRequest([
APIError::BAD_REQUEST => __('Another record already exists. Please specify a different "name".', 'mailpoet'),
]);