Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
59 lines
2.0 KiB
PHP
59 lines
2.0 KiB
PHP
<?php
|
|
namespace Incoviba\Repository;
|
|
|
|
use DateTimeImmutable;
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Implement;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Model;
|
|
|
|
class Job extends Ideal\Repository
|
|
{
|
|
public function getTable(): string
|
|
{
|
|
return 'jobs';
|
|
}
|
|
|
|
public function create(?array $data = null): Model\Job
|
|
{
|
|
$map = (new Implement\Repository\MapperParser())
|
|
->register('configuration', (new Implement\Repository\Mapper())
|
|
->setFunction(function($data) {
|
|
return json_decode($data['configuration'], true);
|
|
}))
|
|
->register('executed', new Implement\Repository\Mapper\Boolean('executed')
|
|
->setDefault(false));
|
|
return $this->parseData(new Model\Job(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Job
|
|
{
|
|
$model->id = $this->saveNew(['configuration', 'executed', 'created_at'],
|
|
[json_encode($model->configuration), $model->executed ? 1 : 0, (new DateTimeImmutable())->format('Y-m-d H:i:s')]);
|
|
return $model;
|
|
}
|
|
public function edit(Define\Model $model, array $new_data): Model\Job
|
|
{
|
|
if (isset($new_data['configuration']) and !is_string($new_data['configuration'])) {
|
|
$new_data['configuration'] = json_encode($new_data['configuration']);
|
|
}
|
|
if (isset($new_data['executed'])) {
|
|
$new_data['executed'] = $new_data['executed'] ? 1 : 0;
|
|
}
|
|
return $this->update($model, ['configuration', 'executed', 'updated_at'],
|
|
array_merge($new_data, ['updated_at' => (new DateTimeImmutable())->format('Y-m-d H:i:s')]));
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchPending(): array
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select()
|
|
->from($this->getTable())
|
|
->where('executed = ?');
|
|
return $this->fetchMany($query, [false]);
|
|
}
|
|
}
|