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