Add check on list type

[MAILPOET-4752]
This commit is contained in:
Jan Lysý
2022-10-31 15:06:58 +01:00
committed by Aschepikov
parent f231f3dc6d
commit ad46d05c6b
4 changed files with 40 additions and 8 deletions

View File

@@ -23,4 +23,5 @@ class APIException extends \Exception {
const LIST_USED_IN_EMAIL = 20;
const LIST_USED_IN_FORM = 21;
const FAILED_TO_DELETE_LIST = 22;
const LIST_TYPE_IS_NOT_SUPPORTED = 23;
}

View File

@@ -60,6 +60,9 @@ class Segments {
// secondly validation on list name
$this->validateSegmentName($data);
// update is supported only for default segment type
$this->validateSegmentType((string)$data['id']);
try {
$segment = $this->segmentsRepository->createOrUpdate(
$data['name'],
@@ -160,6 +163,21 @@ class Segments {
}
}
private function validateSegmentType(string $segmentId): void {
$segment = $this->segmentsRepository->findOneById($segmentId);
if ($segment && $segment->getType() !== SegmentEntity::TYPE_DEFAULT) {
throw new APIException(
str_replace(
'%1$s',
"'" . $segment->getType() . "'",
// translators: %1$s is an invalid segment type.
__('List of the type %1$s is not supported for this action.', 'mailpoet')
),
APIException::LIST_TYPE_IS_NOT_SUPPORTED
);
}
}
/**
* @param SegmentEntity $segment
* @return array