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\Customer;
6use App\Models\CustomerLogin;
7use App\Models\CustomerOptInHistory;
8use Carbon\Carbon;
9use Illuminate\Console\Command;
10use Illuminate\Support\Facades\DB;
11use Illuminate\Support\Facades\Log;
12
13/**
14 * @codeCoverageIgnore
15 */
16class DeleteUnconfirmedCustomers extends Command
17{
18    /**
19     * The name and signature of the console command.
20     *
21     * @var string
22     */
23    protected $signature = 'customer:deleteuc';
24
25    /**
26     * The console command description.
27     *
28     * @var string
29     */
30    protected $description = 'Delete the customers that haven\'t
31    confirmed their acocunt within 24 hours after the registeration.';
32
33    /**
34     * Execute the console command.
35     *
36     * @return integer
37     */
38    public function handle(): int
39    {
40        /** @var \Illuminate\Database\Eloquent\Collection $customersLogins */
41        $customersLogins = CustomerLogin::query()
42            ->where('status', false)
43            ->where('created_at', '<', Carbon::now()->subDay(1))
44            ->with(['brand', 'customer'])
45            ->get();
46        $customersLogins->map(function (CustomerLogin $customerLogin) {
47            Log::info($customerLogin->customer_id);
48            DB::transaction(function () use ($customerLogin) {
49                /** @var \App\Models\Customer $customer */
50                $customer = $customerLogin->customer;
51
52                /** @var \App\Models\Brand */
53                $brand = $customerLogin->brand;
54
55                // * Suppression de ses infos de login pour la marque.
56                $customer->info($brand, Customer::FIRST_OR_FAIL)->deleteOrFail();
57                $customer->login($brand, Customer::FIRST_OR_FAIL)->deleteOrFail();
58
59                // * Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm.
60                if (!$customer->infos()->count()) {
61                    // * Suppression du customer une fois tout le reste supprimé.
62                    $customer->optInHistory()->get()
63                        ->map(function (CustomerOptInHistory $customerOptInHistory) {
64                            $customerOptInHistory->deleteOrFail();
65                        });
66                    $customer->deleteOrFail();
67                }
68            });
69        });
70        return 0;
71    }
72}