Add API method deleteList

[MAILPOET-4752]
This commit is contained in:
Jan Lysý
2022-10-26 18:45:07 +02:00
committed by Aschepikov
parent 25ba667cb1
commit 56c79dd66a
3 changed files with 65 additions and 0 deletions

View File

@ -80,6 +80,10 @@ class API {
return $this->segments->addList($list);
}
public function deleteList(string $listId): bool {
return $this->segments->deleteList($listId);
}
public function updateList(array $list): array {
return $this->segments->updateList($list);
}

View File

@ -20,4 +20,7 @@ class APIException extends \Exception {
const WELCOME_FAILED_TO_SEND = 17;
const LIST_ID_REQUIRED = 18;
const FAILED_TO_UPDATE_LIST = 19;
const LIST_USED_IN_EMAIL = 20;
const LIST_USED_IN_FORM = 21;
const FAILED_TO_DELETE_LIST = 22;
}

View File

@ -3,17 +3,29 @@
namespace MailPoet\API\MP\v1;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Form\FormsRepository;
use MailPoet\Newsletter\Segment\NewsletterSegmentRepository;
use MailPoet\Segments\SegmentsRepository;
class Segments {
private const DATE_FORMAT = 'Y-m-d H:i:s';
/** @var NewsletterSegmentRepository */
private $newsletterSegmentRepository;
/** @var FormsRepository */
private $formsRepository;
/** @var SegmentsRepository */
private $segmentsRepository;
public function __construct (
NewsletterSegmentRepository $newsletterSegmentRepository,
FormsRepository $formsRepository,
SegmentsRepository $segmentsRepository
) {
$this->newsletterSegmentRepository = $newsletterSegmentRepository;
$this->formsRepository = $formsRepository;
$this->segmentsRepository = $segmentsRepository;
}
@ -66,6 +78,52 @@ class Segments {
return $this->buildItem($segment);
}
public function deleteList(string $listId): bool {
$this->validateSegmentId($listId);
$activelyUsedNewslettersSubjects = $this->newsletterSegmentRepository->getSubjectsOfActivelyUsedEmailsForSegments([$listId]);
if (isset($activelyUsedNewslettersSubjects[$listId])) {
throw new APIException(
str_replace(
'%1$s',
"'" . join("', '", $activelyUsedNewslettersSubjects[$listId] ) . "'",
// translators: %1$s is a comma-seperated list of emails for which the segment is used.
_x('List cannot be deleted because its used for %1$s email', 'Alert shown when trying to delete segment, which is assigned to any automatic emails.', 'mailpoet')
),
APIException::LIST_USED_IN_EMAIL
);
}
$activelyUsedFormNames = $this->formsRepository->getNamesOfFormsForSegments();
if (isset($activelyUsedFormNames[$listId])) {
throw new APIException(
str_replace(
'%1$s',
"'" . join("', '", $activelyUsedFormNames[$listId] ) . "'",
// translators: %1$s is a comma-seperated list of forms for which the segment is used.
_nx(
'List cannot be deleted because its used for %1$s form',
'List cannot be deleted because its used for %1$s forms',
count($activelyUsedFormNames[$listId]),
'Alert shown when trying to delete segment, when it is assigned to a form.',
'mailpoet'
)
),
APIException::LIST_USED_IN_FORM
);
}
try {
$this->segmentsRepository->bulkDelete([$listId]);
return true;
} catch (\Exception $e) {
throw new APIException(
__('The list couldnt be deleted from the database', 'mailpoet'),
APIException::FAILED_TO_DELETE_LIST
);
}
}
private function validateSegmentId(string $segmentId): void {
if (empty($segmentId)) {
throw new APIException(