Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| SetServiceStats | n/a |
0 / 0 |
n/a |
0 / 0 |
5 | n/a |
0 / 0 |
|||
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| handle | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| retryUntil | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| middleware | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Models\Service; |
| 6 | use App\Models\ServiceStat; |
| 7 | use Illuminate\Bus\Queueable; |
| 8 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | use Illuminate\Foundation\Bus\Dispatchable; |
| 10 | use Illuminate\Queue\InteractsWithQueue; |
| 11 | use Illuminate\Queue\Middleware\WithoutOverlapping; |
| 12 | use Illuminate\Queue\SerializesModels; |
| 13 | |
| 14 | /** |
| 15 | * @codeCoverageIgnore |
| 16 | */ |
| 17 | class SetServiceStats implements ShouldQueue |
| 18 | { |
| 19 | use Dispatchable; |
| 20 | use InteractsWithQueue; |
| 21 | use Queueable; |
| 22 | use SerializesModels; |
| 23 | |
| 24 | private const KEY = '2202020'; |
| 25 | |
| 26 | /** @var \App\Models\Service */ |
| 27 | protected $service; |
| 28 | |
| 29 | /** |
| 30 | * Create a new job instance. |
| 31 | * |
| 32 | * @param Service $service |
| 33 | * @return void |
| 34 | */ |
| 35 | public function __construct(Service $service) |
| 36 | { |
| 37 | $this->service = $service; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Execute the job. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function handle() |
| 46 | { |
| 47 | $serviceStat = ServiceStat::firstOrNew([ |
| 48 | 'date' => date('Y-m-d'), |
| 49 | 'service_id' => $this->service->id, |
| 50 | 'brand_id' => $this->service->brand->id, |
| 51 | ]); |
| 52 | if (!$serviceStat->exists) { |
| 53 | $serviceStat->service()->associate($this->service); |
| 54 | $serviceStat->brand()->associate($this->service->brand); |
| 55 | } |
| 56 | $serviceStat->nb_calls += 1; |
| 57 | $serviceStat->saveOrFail(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Determine the time at which the job should timeout. |
| 62 | * |
| 63 | * @return \DateTime |
| 64 | */ |
| 65 | public function retryUntil() |
| 66 | { |
| 67 | return now()->addMinutes(rand(0, 360)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the middleware the job should pass through. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function middleware() |
| 76 | { |
| 77 | return [ |
| 78 | (new WithoutOverlapping(self::KEY)) |
| 79 | ->releaseAfter(rand(90, 180)) |
| 80 | ]; |
| 81 | } |
| 82 | } |