AutomationRunLogStorage.php
1 year ago
AutomationRunStorage.php
2 months ago
AutomationStatisticsStorage.php
1 month ago
AutomationStorage.php
2 months ago
index.php
3 years ago
AutomationRunLogStorage.php
135 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Storage; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\AutomationRunLog; |
| 9 | use MailPoet\Automation\Engine\Exceptions; |
| 10 | use MailPoet\InvalidStateException; |
| 11 | |
| 12 | class AutomationRunLogStorage { |
| 13 | /** @var string */ |
| 14 | private $table; |
| 15 | |
| 16 | public function __construct() { |
| 17 | global $wpdb; |
| 18 | $this->table = $wpdb->prefix . 'mailpoet_automation_run_logs'; |
| 19 | } |
| 20 | |
| 21 | public function createAutomationRunLog(AutomationRunLog $automationRunLog): int { |
| 22 | global $wpdb; |
| 23 | $result = $wpdb->insert($this->table, $automationRunLog->toArray()); |
| 24 | if ($result === false) { |
| 25 | $this->throwDatabaseError(); |
| 26 | } |
| 27 | return $wpdb->insert_id; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 28 | } |
| 29 | |
| 30 | public function updateAutomationRunLog(AutomationRunLog $automationRunLog): void { |
| 31 | global $wpdb; |
| 32 | $result = $wpdb->update($this->table, $automationRunLog->toArray(), ['id' => $automationRunLog->getId()]); |
| 33 | if ($result === false) { |
| 34 | $this->throwDatabaseError(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function getAutomationRunStatisticsForAutomationInTimeFrame(int $automationId, string $status, \DateTimeImmutable $after, \DateTimeImmutable $before, ?int $versionId = null): array { |
| 39 | global $wpdb; |
| 40 | $andWhere = $versionId ? 'AND run.version_id = %d' : ''; |
| 41 | $results = $wpdb->get_results( |
| 42 | // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The number of replacements is dynamic. |
| 43 | $wpdb->prepare( |
| 44 | ' |
| 45 | SELECT COUNT(log.id) AS count, log.step_id |
| 46 | FROM %i AS log |
| 47 | JOIN %i AS run ON log.automation_run_id = run.id |
| 48 | WHERE run.automation_id = %d AND log.status = %s AND run.created_at BETWEEN %s AND %s |
| 49 | ' . $andWhere . /* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- The condition uses placeholders. */ ' |
| 50 | GROUP BY log.step_id |
| 51 | ', |
| 52 | array_merge( |
| 53 | [ |
| 54 | $this->table, |
| 55 | $wpdb->prefix . 'mailpoet_automation_runs', |
| 56 | $automationId, |
| 57 | $status, |
| 58 | $after->format('Y-m-d H:i:s'), |
| 59 | $before->format('Y-m-d H:i:s'), |
| 60 | ], |
| 61 | $versionId ? [$versionId] : [] |
| 62 | ) |
| 63 | ), |
| 64 | ARRAY_A |
| 65 | ); |
| 66 | return is_array($results) ? $results : []; |
| 67 | } |
| 68 | |
| 69 | public function getAutomationRunLog(int $id): ?AutomationRunLog { |
| 70 | global $wpdb; |
| 71 | |
| 72 | $result = $wpdb->get_row( |
| 73 | $wpdb->prepare('SELECT * FROM %i WHERE id = %d', $this->table, $id), |
| 74 | ARRAY_A |
| 75 | ); |
| 76 | |
| 77 | if ($result) { |
| 78 | $data = (array)$result; |
| 79 | return AutomationRunLog::fromArray($data); |
| 80 | } |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | public function getAutomationRunLogByRunAndStepId(int $runId, string $stepId): ?AutomationRunLog { |
| 85 | global $wpdb; |
| 86 | $result = $wpdb->get_row( |
| 87 | $wpdb->prepare('SELECT * FROM %i WHERE automation_run_id = %d AND step_id = %s', $this->table, $runId, $stepId), |
| 88 | ARRAY_A |
| 89 | ); |
| 90 | return $result ? AutomationRunLog::fromArray((array)$result) : null; |
| 91 | } |
| 92 | |
| 93 | /** @return AutomationRunLog[] */ |
| 94 | public function getLogsForAutomationRun(int $automationRunId): array { |
| 95 | global $wpdb; |
| 96 | |
| 97 | $results = $wpdb->get_results( |
| 98 | $wpdb->prepare( |
| 99 | ' |
| 100 | SELECT * |
| 101 | FROM %i |
| 102 | WHERE automation_run_id = %d |
| 103 | ORDER BY id ASC |
| 104 | ', |
| 105 | $this->table, |
| 106 | $automationRunId |
| 107 | ), |
| 108 | ARRAY_A |
| 109 | ); |
| 110 | |
| 111 | if (!is_array($results)) { |
| 112 | throw InvalidStateException::create(); |
| 113 | } |
| 114 | |
| 115 | if ($results) { |
| 116 | return array_map(function($data) { |
| 117 | /** @var array $data - for PHPStan because it conflicts with expected callable(mixed): mixed)|null */ |
| 118 | return AutomationRunLog::fromArray($data); |
| 119 | }, $results); |
| 120 | } |
| 121 | |
| 122 | return []; |
| 123 | } |
| 124 | |
| 125 | public function truncate(): void { |
| 126 | global $wpdb; |
| 127 | $wpdb->query($wpdb->prepare('TRUNCATE %i', $this->table)); |
| 128 | } |
| 129 | |
| 130 | private function throwDatabaseError(): void { |
| 131 | global $wpdb; |
| 132 | throw Exceptions::databaseError($wpdb->last_error); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 133 | } |
| 134 | } |
| 135 |