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()
39    {
40        /** @var \Illuminate\Database\Eloquent\Collection $customersLogins */
41        $customersLogins = CustomerLogin::whereStatus(false)
42            ->where('created_at', '<', Carbon::now()->subDay(1))
43            ->with(['brand', 'customer'])
44            ->get();
45        $customersLogins->map(function (CustomerLogin $customerLogin) {
46            Log::info($customerLogin->customer_id);
47            DB::transaction(function () use ($customerLogin) {
48                /** @var \App\Models\Customer $customer */
49                $customer = $customerLogin->customer;
50
51                /** @var \App\Models\Brand */
52                $brand = $customerLogin->brand;
53
54                // * Suppression de ses infos de login pour la marque.
55                $customer->info($brand, Customer::FIRST_OR_FAIL)->deleteOrFail();
56                $customer->login($brand, Customer::FIRST_OR_FAIL)->deleteOrFail();
57
58                // * Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm.
59                if (!$customer->infos()->count()) {
60                    // * Suppression du customer une fois tout le reste supprimé.
61                    $customer->optInHistory()->get()
62                        ->map(function (CustomerOptInHistory $customerOptInHistory) {
63                            $customerOptInHistory->deleteOrFail();
64                        });
65                    $customer->deleteOrFail();
66                }
67            });
68        });
69        return 0;
70    }
71}