Use Doctrine model in trash, restore and delete

[MAILPOET-3169]
This commit is contained in:
Jan Lysý
2021-02-01 09:46:31 +01:00
committed by Veljko V
parent aff7de4659
commit b03f301a5a
2 changed files with 20 additions and 26 deletions

View File

@ -13,7 +13,6 @@ use MailPoet\Doctrine\Validator\ValidationException;
use MailPoet\Entities\SegmentEntity; use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\SubscriberEntity; use MailPoet\Entities\SubscriberEntity;
use MailPoet\Listing; use MailPoet\Listing;
use MailPoet\Models\Segment;
use MailPoet\Segments\SegmentListingRepository; use MailPoet\Segments\SegmentListingRepository;
use MailPoet\Segments\SegmentSaveController; use MailPoet\Segments\SegmentSaveController;
use MailPoet\Segments\SegmentsRepository; use MailPoet\Segments\SegmentsRepository;
@ -116,23 +115,21 @@ class Segments extends APIEndpoint {
} }
public function restore($data = []) { public function restore($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false); $segment = $this->getSegment($data);
$segment = Segment::findOne($id); if ($segment instanceof SegmentEntity) {
if ($segment instanceof Segment) {
// When the segment is of type WP_USERS we want to restore all its subscribers // When the segment is of type WP_USERS we want to restore all its subscribers
if ($segment->type === SegmentEntity::TYPE_WP_USERS) { if ($segment->getType() === SegmentEntity::TYPE_WP_USERS) {
$subscribers = $this->subscribersRepository->findBySegment((int)$segment->id); $subscribers = $this->subscribersRepository->findBySegment((int)$segment->getId());
$subscriberIds = array_map(function (SubscriberEntity $subscriberEntity): int { $subscriberIds = array_map(function (SubscriberEntity $subscriberEntity): int {
return (int)$subscriberEntity->getId(); return (int)$subscriberEntity->getId();
}, $subscribers); }, $subscribers);
$this->subscribersRepository->bulkRestore($subscriberIds); $this->subscribersRepository->bulkRestore($subscriberIds);
} }
$segment->restore(); $this->segmentsRepository->bulkRestore([$segment->getId()]);
$segment = Segment::findOne($segment->id); $this->segmentsRepository->refresh($segment);
if(!$segment instanceof Segment) return $this->errorResponse();
return $this->successResponse( return $this->successResponse(
$segment->asArray(), $this->segmentsResponseBuilder->build($segment),
['count' => 1] ['count' => 1]
); );
} else { } else {
@ -143,23 +140,21 @@ class Segments extends APIEndpoint {
} }
public function trash($data = []) { public function trash($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false); $segment = $this->getSegment($data);
$segment = Segment::findOne($id); if ($segment instanceof SegmentEntity) {
if ($segment instanceof Segment) {
// When the segment is of type WP_USERS we want to trash all subscribers who aren't subscribed in another list // When the segment is of type WP_USERS we want to trash all subscribers who aren't subscribed in another list
if ($segment->type === SegmentEntity::TYPE_WP_USERS) { if ($segment->getType() === SegmentEntity::TYPE_WP_USERS) {
$subscribers = $this->subscribersRepository->findExclusiveSubscribersBySegment((int)$segment->id); $subscribers = $this->subscribersRepository->findExclusiveSubscribersBySegment((int)$segment->getId());
$subscriberIds = array_map(function (SubscriberEntity $subscriberEntity): int { $subscriberIds = array_map(function (SubscriberEntity $subscriberEntity): int {
return (int)$subscriberEntity->getId(); return (int)$subscriberEntity->getId();
}, $subscribers); }, $subscribers);
$this->subscribersRepository->bulkTrash($subscriberIds); $this->subscribersRepository->bulkTrash($subscriberIds);
} }
$segment->trash(); $this->segmentsRepository->bulkTrash([$segment->getId()]);
$segment = Segment::findOne($segment->id); $this->segmentsRepository->refresh($segment);
if(!$segment instanceof Segment) return $this->errorResponse();
return $this->successResponse( return $this->successResponse(
$segment->asArray(), $this->segmentsResponseBuilder->build($segment),
['count' => 1] ['count' => 1]
); );
} else { } else {
@ -170,10 +165,9 @@ class Segments extends APIEndpoint {
} }
public function delete($data = []) { public function delete($data = []) {
$id = (isset($data['id']) ? (int)$data['id'] : false); $segment = $this->getSegment($data);
$segment = Segment::findOne($id); if ($segment instanceof SegmentEntity) {
if ($segment instanceof Segment) { $this->segmentsRepository->bulkDelete([$segment->getId()]);
$segment->delete();
return $this->successResponse(null, ['count' => 1]); return $this->successResponse(null, ['count' => 1]);
} else { } else {
return $this->errorResponse([ return $this->errorResponse([
@ -206,7 +200,7 @@ class Segments extends APIEndpoint {
public function synchronize($data) { public function synchronize($data) {
try { try {
if ($data['type'] === Segment::TYPE_WC_USERS) { if ($data['type'] === SegmentEntity::TYPE_WC_USERS) {
$this->wooCommerceSync->synchronizeCustomers(); $this->wooCommerceSync->synchronizeCustomers();
} else { } else {
$this->wpSegment->synchronizeUsers(); $this->wpSegment->synchronizeUsers();

View File

@ -121,10 +121,10 @@ class SegmentsRepository extends Repository {
$rows = $this->entityManager->createQueryBuilder()->update(SegmentEntity::class, 's') $rows = $this->entityManager->createQueryBuilder()->update(SegmentEntity::class, 's')
->set('s.deletedAt', ':deletedAt') ->set('s.deletedAt', ':deletedAt')
->where('s.id IN (:ids)') ->where('s.id IN (:ids)')
->andWhere('s.type = :typeDefault') ->andWhere('s.type IN (:types)')
->setParameter('deletedAt', $deletedAt) ->setParameter('deletedAt', $deletedAt)
->setParameter('ids', $ids) ->setParameter('ids', $ids)
->setParameter('typeDefault', SegmentEntity::TYPE_DEFAULT) ->setParameter('types', [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS])
->getQuery()->execute(); ->getQuery()->execute();
return $rows; return $rows;