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