Add new API method updateList

[MAILPOET-4752]
This commit is contained in:
Jan Lysý
2022-10-25 20:13:48 +02:00
committed by Aschepikov
parent 5fba25d823
commit fbf4c853c1
3 changed files with 43 additions and 0 deletions

View File

@ -80,6 +80,10 @@ class API {
return $this->segments->addList($list);
}
public function updateList(array $list): array {
return $this->segments->updateList($list);
}
public function getSubscriber($subscriberEmail) {
return $this->subscribers->getSubscriber($subscriberEmail);
}

View File

@ -18,4 +18,6 @@ class APIException extends \Exception {
const LIST_EXISTS = 15;
const FAILED_TO_SAVE_LIST = 16;
const WELCOME_FAILED_TO_SEND = 17;
const LIST_ID_REQUIRED = 18;
const FAILED_TO_UPDATE_LIST = 19;
}

View File

@ -41,6 +41,43 @@ class Segments {
return $this->buildItem($segment);
}
public function updateList(array $data): array {
// firstly validation on list id
if (empty($data['id'])) {
throw new APIException(
__('List id is required.', 'mailpoet'),
APIException::LIST_ID_REQUIRED
);
}
if (!$this->segmentsRepository->findOneById((string)$data['id'])) {
throw new APIException(
__('The list does not exist.', 'mailpoet'),
APIException::LIST_NOT_EXISTS
);
}
// secondly validation on list name
$this->validateSegmentName($data);
try {
$segment = $this->segmentsRepository->createOrUpdate(
$data['name'],
$data['description'] ?? '',
SegmentEntity::TYPE_DEFAULT,
[],
$data['id']
);
} catch (\Exception $e) {
throw new APIException(
__('The list couldnt be updated in the database', 'mailpoet'),
APIException::FAILED_TO_UPDATE_LIST
);
}
return $this->buildItem($segment);
}
/**
* Throws an exception when the segment's name is invalid
* @return void