setFactory($factory) ->setTable('attachments_jobs'); } protected Factory\Model $factory; public function getFactory(): Factory\Model { return $this->factory; } public function setFactory(Factory\Model $factory): Job { $this->factory = $factory; return $this; } public function install(): void { $query = " CREATE TABLE {$this->getTable()} ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `command` VARCHAR(100) NOT NULL, `arguments` TEXT NOT NULL, PRIMARY KEY (`id`) )"; $this->getConnection()->query($query); } protected function fieldsForUpdate(): array { return $this->fieldsForInsert(); } protected function valuesForUpdate(ModelInterface $model): array { return $this->valuesForInsert($model); } protected function fieldsForInsert(): array { return [ 'command', 'arguments', ]; } protected function valuesForInsert(ModelInterface $model): array { return [ $model->getCommand(), $model->getArguments(), ]; } protected function defaultFind(ModelInterface $model): ModelInterface { return $this->fetchByCommandAndArguments($model->getCommand(), $model->getArguments()); } protected function fieldsForCreate(): array { return [ 'command', 'arguments', ]; } protected function valuesForCreate(array $data): array { return [ $data['command'], $data['arguments'], ]; } protected function defaultSearch(array $data): ModelInterface { return $this->fetchByCommandAndArguments($data['command'], $data['arguments']); } public function load(array $row): ModelInterface { return (new BaseModel()) ->setId($row['id']) ->setCommand($row['command']) ->setArguments($row['arguments']) ->setStateRepository($this->getFactory()->find(Emails\Model\State\Job::class)); } public function fetchAllPending(): array { $query = "SELECT a.* FROM {$this->getTable()} a JOIN `jobs_states` b ON b.job_id = a.id WHERE b.`status` = ?"; return $this->fetchMany($query); } public function fetchByCommandAndArguments(string $command, string $arguments): \ProVM\Emails\Model\Job { $query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?"; return $this->fetchOne($query, [$command, $arguments]); } public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job { $query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0"; return $this->fetchOne($query, [$message_id]); } }