Files
contabilidad/api/common/Service/Queuer.php

47 lines
1.7 KiB
PHP

<?php
namespace Contabilidad\Common\Service;
use ProVM\Common\Factory\Model as Factory;
use Contabilidad\Queue;
use Contabilidad\QueueArgument;
class Queuer {
protected Factory $factory;
public function __construct(Factory $factory) {
$this->setFactory($factory);
}
public function setFactory(Factory $factory): Queuer {
$this->factory = $factory;
return $this;
}
public function queue(string $command, array $arguments = []) {
if ($this->isProcessed($command, $arguments)) {
return;
}
$queue = $this->factory->create(Queue::class, ['command' => $command, 'created' => (new \DateTime('now'))->format('Y-m-d H:i:s')]);
$queue->save();
if (count($arguments) > 0) {
foreach ($arguments as $argument => $value) {
$arg = $this->factory->create(QueueArgument::class, ['queue_id' => $queue->id, 'argument' => $argument, 'value' => $value]);
$arg->save();
}
}
}
public function isProcessed(string $command, array $arguments = []): bool {
$queues = $this->find($command, $arguments);
if ($queues == null or count($queues) === 0) {
return false;
}
return true;
}
public function find(string $command, array $arguments = []): ?array {
$queues = $this->factory->find(Queue::class)->where([['command', $command], ['processed', 0]])->many();
if ($queues === null) {
return null;
}
if (count($arguments) > 0) {
$queues = array_filter($queues, function($item) use ($arguments) {return count($arguments) === count($item->matchArguments($arguments));});
}
return $queues;
}
}