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