Refactor resolving actionable bulk action ids to listing repository
[MAILPOET-2898]
This commit is contained in:
committed by
Veljko V
parent
91d2d92bbd
commit
8a78322854
@ -352,12 +352,8 @@ class Newsletters extends APIEndpoint {
|
||||
}
|
||||
|
||||
public function bulkAction($data = []) {
|
||||
if (isset($data['listing']['selection']) && is_array($data['listing']['selection'])) {
|
||||
$ids = array_map('intval', $data['listing']['selection']);
|
||||
} else {
|
||||
$definition = $this->listingHandler->getListingDefinition($data);
|
||||
$ids = $this->newsletterListingRepository->getIds($definition);
|
||||
}
|
||||
$definition = $this->listingHandler->getListingDefinition($data['listing']);
|
||||
$ids = $this->newsletterListingRepository->getActionableIds($definition);
|
||||
if ($data['action'] === 'trash') {
|
||||
$this->newslettersRepository->bulkTrash($ids);
|
||||
} elseif ($data['action'] === 'restore') {
|
||||
|
@ -104,7 +104,8 @@ class Handler {
|
||||
$data['sort_by'],
|
||||
$data['sort_order'],
|
||||
$data['offset'],
|
||||
$data['limit']
|
||||
$data['limit'],
|
||||
$data['selection'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,9 @@ class ListingDefinition {
|
||||
/** @var int */
|
||||
private $limit;
|
||||
|
||||
/** @var int[] */
|
||||
private $selection;
|
||||
|
||||
public function __construct(
|
||||
string $group = null,
|
||||
array $filters,
|
||||
@ -35,7 +38,8 @@ class ListingDefinition {
|
||||
string $sortBy,
|
||||
string $sortOrder,
|
||||
int $offset,
|
||||
int $limit
|
||||
int $limit,
|
||||
array $selection = []
|
||||
) {
|
||||
$this->group = $group;
|
||||
$this->filters = $filters;
|
||||
@ -45,6 +49,7 @@ class ListingDefinition {
|
||||
$this->sortOrder = $sortOrder;
|
||||
$this->offset = $offset;
|
||||
$this->limit = $limit;
|
||||
$this->selection = array_map('intval', $selection);
|
||||
}
|
||||
|
||||
/** @return string|null */
|
||||
@ -80,4 +85,8 @@ class ListingDefinition {
|
||||
public function getLimit(): int {
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
public function getSelection(): array {
|
||||
return $this->selection;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,11 @@ abstract class ListingRepository {
|
||||
return (int)$queryBuilder->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function getIds(ListingDefinition $definition): array {
|
||||
public function getActionableIds(ListingDefinition $definition): array {
|
||||
$ids = $definition->getSelection();
|
||||
if (!empty($ids)) {
|
||||
return $ids;
|
||||
}
|
||||
$queryBuilder = clone $this->queryBuilder;
|
||||
$this->applyFromClause($queryBuilder);
|
||||
$this->applyConstraints($queryBuilder, $definition);
|
||||
|
43
tests/unit/Listing/HandlerTest.php
Normal file
43
tests/unit/Listing/HandlerTest.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Listing;
|
||||
|
||||
class HandlerTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var Handler */
|
||||
private $handler;
|
||||
|
||||
private $listingData = [
|
||||
'params' => [
|
||||
0 => 'page[1]/sort_by[sent_at]/sort_order[desc]/group[sent]',
|
||||
'type' => 'standard',
|
||||
],
|
||||
'offset' => '0',
|
||||
'limit' => '20',
|
||||
'group' => 'sent',
|
||||
'search' => 'abcd',
|
||||
'sort_by' => 'sent_at',
|
||||
'sort_order' => 'desc',
|
||||
'selection' => ['1','2'],
|
||||
];
|
||||
|
||||
public function _before() {
|
||||
parent::_before();
|
||||
$this->handler = new Handler();
|
||||
}
|
||||
|
||||
public function testItCreatesListingDefinition() {
|
||||
$definition = $this->handler->getListingDefinition($this->listingData);
|
||||
expect($definition->getSearch())->equals('abcd');
|
||||
expect($definition->getGroup())->equals('sent');
|
||||
expect($definition->getOffset())->equals(0);
|
||||
expect($definition->getLimit())->equals(20);
|
||||
expect($definition->getSortBy())->equals('sent_at');
|
||||
expect($definition->getSortOrder())->equals('desc');
|
||||
expect($definition->getParameters())->equals([
|
||||
0 => 'page[1]/sort_by[sent_at]/sort_order[desc]/group[sent]',
|
||||
'type' => 'standard',
|
||||
]);
|
||||
expect($definition->getSelection())->equals([1, 2]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user