Files
piratepoet/mailpoet/lib/Automation/Engine/Storage/WorkflowStorage.php
Jan Jakes 3a22521a55 Add json encode/decode wrapper class
[MAILPOET-4136]
2022-03-14 09:36:21 +01:00

73 lines
2.1 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Storage;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Utils\Json;
use MailPoet\Automation\Engine\Workflows\Trigger;
use MailPoet\Automation\Engine\Workflows\Workflow;
use wpdb;
class WorkflowStorage {
/** @var string */
private $table;
/** @var wpdb */
private $wpdb;
public function __construct() {
global $wpdb;
$this->table = $wpdb->prefix . 'mailpoet_automation_workflows';
$this->wpdb = $wpdb;
}
public function createWorkflow(Workflow $workflow): int {
$result = $this->wpdb->insert($this->table, $workflow->toArray());
if (!$result) {
throw Exceptions::databaseError($this->wpdb->last_error);
}
return $this->wpdb->insert_id;
}
public function getWorkflow(int $id): ?Workflow {
$query = strval($this->wpdb->prepare("SELECT * FROM $this->table WHERE id = %d", $id));
$data = $this->wpdb->get_row($query, ARRAY_A);
return $data ? Workflow::fromArray((array)$data) : null;
}
/** @return string[] */
public function getActiveTriggerKeys(): array {
$query = strval(
$this->wpdb->prepare(
"SELECT DISTINCT trigger_keys FROM $this->table WHERE status = %s",
Workflow::STATUS_ACTIVE
)
);
$result = $this->wpdb->get_col($query);
$triggerKeys = [];
foreach ($result as $item) {
/** @var string[] $keys */
$keys = Json::decode($item);
$triggerKeys = array_merge($triggerKeys, $keys);
}
return array_unique($triggerKeys);
}
/** @return Workflow[] */
public function getActiveWorkflowsByTrigger(Trigger $trigger): array {
$query = strval(
$this->wpdb->prepare(
"SELECT * FROM $this->table WHERE status = %s AND trigger_keys LIKE %s",
Workflow::STATUS_ACTIVE,
'%' . $this->wpdb->esc_like($trigger->getKey()) . '%'
)
);
$data = $this->wpdb->get_results($query, ARRAY_A);
return array_map(function (array $workflowData) {
return Workflow::fromArray($workflowData);
}, (array)$data);
}
}