Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
DeleteUnconfirmedCustomers
n/a
0 / 0
n/a
0 / 0
3
n/a
0 / 0
 handle
n/a
0 / 0
3
n/a
0 / 0
1<?php
2
3namespace App\Console\Commands;
4
5use App\Models\Customer;
6use App\Models\CustomerLogin;
7use Carbon\Carbon;
8use Illuminate\Console\Command;
9
10/**
11 * @codeCoverageIgnore
12 */
13class DeleteUnconfirmedCustomers extends Command
14{
15    /**
16     * The name and signature of the console command.
17     *
18     * @var string
19     */
20    protected $signature = 'customer:deleteuc';
21
22    /**
23     * The console command description.
24     *
25     * @var string
26     */
27    protected $description = 'Delete the customers that haven\'t
28    confirmed their acocunt within 24 hours after the registeration.';
29
30    /**
31     * Execute the console command.
32     *
33     * @return integer
34     */
35    public function handle()
36    {
37        $customersLogins = CustomerLogin::where([['status', 0], ['created_at', '<', Carbon::now()->subDay(1)]])->get();
38        foreach ($customersLogins as $customerLogin) {
39            // Si non activé et créer il y a plus d'un jour : Suppression.
40            $customerLogin->customer->infos()->where('brand_id', $customerLogin->brand_id)->first()->delete();
41            // On récupère son id pour le cas ou on doit le supprimer complètement après.
42            $customerId = $customerLogin->customer->id;
43            // Suppression de ses infos de login pour la marque.
44            $customerLogin->delete();
45            // Si le customer n'a plus d'info il n'a donc plus besoin de compte en crm.
46            if (count($customerLogin->customer->infos) === 0) {
47                // Suppression du customer une fois tout le reste supprimé.
48                $customer = Customer::find($customerId);
49                $customer->optInHistory()->delete();
50                $customer->delete();
51            }
52        }
53        return 0;
54    }
55}