| 1 |
<?php |
| 2 |
|
| 3 |
namespace Depicter\Database\Repository; |
| 4 |
|
| 5 |
use Depicter\Database\Entity\QueueJob; |
| 6 |
use Depicter\Utility\Sanitize; |
| 7 |
|
| 8 |
class QueueJobRepository |
| 9 |
{ |
| 10 |
|
| 11 |
private QueueJob $job; |
| 12 |
|
| 13 |
public function __construct(){ |
| 14 |
$this->job = QueueJob::new(); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Access to an instance of Lead entity |
| 19 |
* |
| 20 |
* @return QueueJob |
| 21 |
*/ |
| 22 |
public function job(): QueueJob{ |
| 23 |
return QueueJob::new(); |
| 24 |
} |
| 25 |
|
| 26 |
public function create( $queue, $payload ) { |
| 27 |
return $this->job()->create([ |
| 28 |
'queue' => $queue, |
| 29 |
'payload' => $payload, |
| 30 |
'attempts' => 0, |
| 31 |
]); |
| 32 |
} |
| 33 |
|
| 34 |
public function update( $id, array $fields = [] ) { |
| 35 |
if ( empty( $fields ) ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
$job = $this->job()->findById( $id ); |
| 40 |
|
| 41 |
if ( $job && $job->count() ){ |
| 42 |
return $job->first()->update($fields); |
| 43 |
} |
| 44 |
|
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
public function delete( $id ): bool |
| 49 |
{ |
| 50 |
$succeed = false; |
| 51 |
|
| 52 |
if( is_array( $id ) ){ |
| 53 |
$ids = $id; |
| 54 |
} elseif( false !== strpos( $id, ',' ) ){ |
| 55 |
$ids = explode(',', $id ); |
| 56 |
} else { |
| 57 |
$ids = [$id]; |
| 58 |
} |
| 59 |
|
| 60 |
foreach( $ids as $id ){ |
| 61 |
$id = Sanitize::int( $id ); |
| 62 |
if( $job = $this->job()->findById( $id ) ){ |
| 63 |
$job->delete(); |
| 64 |
$succeed = true; |
| 65 |
} |
| 66 |
} |
| 67 |
return $succeed; |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|