Refactor MailPoet\API\MP\v1\API::addList to doctrine
[MAILPOET-4294]
This commit is contained in:
committed by
Veljko V
parent
73f51523f3
commit
9c5dca0206
@@ -191,43 +191,7 @@ class API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function addList(array $list) {
|
public function addList(array $list) {
|
||||||
// throw exception when list name is missing
|
return $this->segments->addList($list);
|
||||||
if (empty($list['name'])) {
|
|
||||||
throw new APIException(
|
|
||||||
__('List name is required.', 'mailpoet'),
|
|
||||||
APIException::LIST_NAME_REQUIRED
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// throw exception when list already exists
|
|
||||||
if (Segment::where('name', $list['name'])->findOne()) {
|
|
||||||
throw new APIException(
|
|
||||||
__('This list already exists.', 'mailpoet'),
|
|
||||||
APIException::LIST_EXISTS
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// filter out all incoming data that we don't want to change, like type,
|
|
||||||
$list = array_intersect_key($list, array_flip(['name', 'description']));
|
|
||||||
|
|
||||||
// add list
|
|
||||||
$newList = Segment::create();
|
|
||||||
$newList->hydrate($list);
|
|
||||||
$newList->save();
|
|
||||||
if ($newList->getErrors() !== false) {
|
|
||||||
throw new APIException(
|
|
||||||
__(sprintf('Failed to add list: %s', strtolower(implode(', ', $newList->getErrors()))), 'mailpoet'),
|
|
||||||
APIException::FAILED_TO_SAVE_LIST
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reload list to get the saved created|updated|delete dates/other fields
|
|
||||||
$newList = Segment::findOne($newList->id);
|
|
||||||
if (!$newList instanceof Segment) {
|
|
||||||
throw new APIException(__('Failed to add list', 'mailpoet'), APIException::FAILED_TO_SAVE_LIST);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $newList->asArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubscriber($subscriberEmail) {
|
public function getSubscriber($subscriberEmail) {
|
||||||
|
@@ -26,6 +26,25 @@ class Segments {
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addList(array $data): array {
|
||||||
|
if (empty($data['name'])) {
|
||||||
|
throw new APIException(
|
||||||
|
__('List name is required.', 'mailpoet'),
|
||||||
|
APIException::LIST_NAME_REQUIRED
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->segmentsRepository->isNameUnique($data['name'], null)) {
|
||||||
|
throw new APIException(
|
||||||
|
__('This list already exists.', 'mailpoet'),
|
||||||
|
APIException::LIST_EXISTS
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$segment = $this->segmentsRepository->createOrUpdate($data['name'], $data['description'] ?? '');
|
||||||
|
return $this->buildItem($segment);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SegmentEntity $segment
|
* @param SegmentEntity $segment
|
||||||
* @return array
|
* @return array
|
||||||
|
Reference in New Issue
Block a user