Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StatJob
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
 middleware
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Jobs;
4
5use App\Models\Brand;
6use App\Models\BrandStat;
7use App\Models\CustomerInfo;
8use App\Models\CustomerLogin;
9use App\Models\SingleOptin;
10use Illuminate\Bus\Queueable;
11use Illuminate\Contracts\Queue\ShouldQueue;
12use Illuminate\Database\Eloquent\Builder;
13use Illuminate\Foundation\Bus\Dispatchable;
14use Illuminate\Queue\InteractsWithQueue;
15use Illuminate\Queue\Middleware\WithoutOverlapping;
16use Illuminate\Queue\SerializesModels;
17use Illuminate\Support\Carbon;
18
19class StatJob implements ShouldQueue
20{
21    use Dispatchable;
22    use InteractsWithQueue;
23    use Queueable;
24    use SerializesModels;
25
26    /**
27     * Set high max tries as using prevent overlap
28     *
29     * @var integer
30     */
31    public $tries = 10;
32
33    /** Used to prevent overlaping */
34    private const JOBKEY = 32644296;
35
36    /** @var \App\Models\Brand */
37    private $brand;
38
39    /**
40     * Create a new job instance.
41     *
42     * @param \App\Models\Brand $brand
43     * @return void
44     */
45    public function __construct(Brand $brand)
46    {
47        $this->brand = $brand;
48    }
49
50    /**
51     * Execute the job.
52     *
53     * @return void
54     */
55    public function handle(): void
56    {
57        $stats = new BrandStat([
58            'active' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
59                $q->where('id', $this->brand->id);
60            })->whereDate('last_connection', '<', Carbon::now()->subMonths(6))->count(),
61            'inactive' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
62                $q->where('id', $this->brand->id);
63            })
64                ->whereDate('last_connection', '>', Carbon::now()->subMonths(6))->count(),
65            'new_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
66                $q->where('id', $this->brand->id);
67            })
68                ->whereDate('created_at', '<', Carbon::now()->subMonths(6))->count(),
69            'old_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
70                $q->where('id', $this->brand->id);
71            })
72                ->whereDate('created_at', '>', Carbon::now()->subMonths(6))->count(),
73            'total_accounts' => CustomerLogin::query()->whereHas('brand', function (Builder $q) {
74                $q->where('id', $this->brand->id);
75            })->count(),
76            'total_opt_in' => CustomerInfo::query()->whereHas('brand', function (Builder $q) {
77                $q->where('id', $this->brand->id);
78            })->whereOptIn(true)->count() + SingleOptin::query()->whereHas('brand', function (Builder $q) {
79                $q->where('id', $this->brand->id);
80            })->count()
81        ]);
82        $stats->brand()->associate($this->brand);
83        $stats->saveOrFail();
84    }
85
86    /**
87     * Get the middleware the job should pass through.
88     *
89     * @return array
90     */
91    public function middleware(): array
92    {
93        // * Dispatch randomly overlaping jobs if overlapping.
94        return [(new WithoutOverlapping(self::JOBKEY))->releaseAfter(rand(20, 90))];
95    }
96}