Files
piratepoet/lib/Segments/BulkAction.php
Rostislav Wolny a69ae0eea7 Refactor listing handler to reusable service
[MAILPOET-1689]
2018-12-17 15:02:42 +01:00

55 lines
1.4 KiB
PHP

<?php
namespace MailPoet\Segments;
use MailPoet\Listing\Handler;
use MailPoet\Models\Segment;
use MailPoet\WP\Hooks;
class BulkAction {
private $data = null;
function __construct($data) {
$this->data = $data;
}
/**
* @return array
* @throws \Exception
*/
function apply() {
if(!isset($this->data['listing']['filter']['segment'])) {
throw new \InvalidArgumentException('Missing segment id');
}
$segment = Segment::findOne($this->data['listing']['filter']['segment']);
if($segment) {
$segment = $segment->asArray();
}
return $this->applySegment($segment);
}
/**
* @param array $segment
*
* @return array
* @throws \Exception
*/
private function applySegment($segment) {
if(!$segment || $segment['type'] === Segment::TYPE_DEFAULT || $segment['type'] === Segment::TYPE_WP_USERS) {
$bulk_action = new \MailPoet\Listing\BulkActionController(new Handler());
return $bulk_action->apply('\MailPoet\Models\Subscriber', $this->data);
} else {
$handlers = Hooks::applyFilters('mailpoet_subscribers_in_segment_apply_bulk_action_handlers', array());
foreach($handlers as $handler) {
$meta = $handler->apply($segment, $this->data);
if($meta) {
return $meta;
}
}
throw new \InvalidArgumentException('No handler found for segment');
}
}
}