Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
DeleteUnconfirmedCustomers
n/a
0 / 0
n/a
0 / 0
2
n/a
0 / 0
 handle
n/a
0 / 0
n/a
0 / 0
2
1<?php
2
3namespace App\Console\Commands;
4
5use App\Models\CustomerInfo;
6use App\Models\CustomerLogin;
7use App\Models\CustomerOptInHistory;
8use Carbon\Carbon;
9use Illuminate\Console\Command;
10use Illuminate\Support\Facades\DB;
11
12/**
13 * @codeCoverageIgnore
14 */
15class DeleteUnconfirmedCustomers extends Command
16{
17    /**
18     * The name and signature of the console command.
19     *
20     * @var string
21     */
22    protected $signature = 'customer:deleteuc';
23
24    /**
25     * The console command description.
26     *
27     * @var string
28     */
29    protected $description = 'Delete the customers that haven\'t
30    confirmed their acocunt within 24 hours after the registeration.';
31
32    /**
33     * Execute the console command.
34     *
35     * @return integer
36     */
37    public function handle()
38    {
39        /** @var \Illuminate\Database\Eloquent\Collection $customersLogins */
40        $customersLogins = CustomerLogin::whereStatus(false)->where('created_at', '<', Carbon::now()->subDay(1))->get();
41        $customersLogins->map(function (CustomerLogin $customerLogin) {
42            DB::transaction(function () use ($customerLogin) {
43                /** @var \App\Models\Customer $customer */
44                $customer = $customerLogin->customer;
45
46                // * Si non activé et créer il y a plus d'un jour : Suppression.
47                $customerLogin->customer->infos()->where('brand_id', $customerLogin->brand_id)
48                    ->get()->map(function (CustomerInfo $customerInfo) {
49                        $customerInfo->deleteOrFail();
50                    });
51
52                // * Suppression de ses infos de login pour la marque.
53                $customerLogin->deleteOrFail();
54
55                // * Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm.
56                if (!$customer->infos()->count()) {
57                    // * Suppression du customer une fois tout le reste supprimé.
58                    $customer->optInHistory()->get()
59                        ->map(function (CustomerOptInHistory $customerOptInHistory) {
60                            $customerOptInHistory->deleteOrFail();
61                        });
62                    $customer->deleteOrFail();
63                }
64            });
65        });
66        return 0;
67    }
68}