Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
MigrateOldData
n/a
0 / 0
n/a
0 / 0
19
n/a
0 / 0
 runMigration
n/a
0 / 0
n/a
0 / 0
1
 instruct
n/a
0 / 0
n/a
0 / 0
15
 initFile
n/a
0 / 0
n/a
0 / 0
1
 write
n/a
0 / 0
n/a
0 / 0
2
1<?php
2
3namespace App\Lib;
4
5use App\Exceptions\AbortRowImportException;
6use App\Models\Brand;
7use App\Models\Customer;
8use App\Models\CustomerInfo;
9use App\Models\CustomerLogin;
10use App\Models\SingleOptin;
11use Carbon\Exceptions\InvalidFormatException;
12use Database\migrations\LeGaulois2016Migration;
13use Illuminate\Database\Eloquent\Builder;
14use Illuminate\Support\Carbon;
15use Illuminate\Support\Facades\File;
16use Illuminate\Support\Facades\Hash;
17use Illuminate\Support\Facades\Validator;
18use Illuminate\Support\Str;
19
20/**
21 * @codeCoverageIgnore
22 */
23class MigrateOldData extends LeGaulois2016Migration
24{
25    /** @var \App\Models\Brand */
26    private $brand;
27
28    /** @var string */
29    private $table;
30
31    /**
32     * RunMigration.
33     *
34     * @param \App\Models\Brand $brand
35     * @param string            $table
36     * @return void
37     */
38    public function runMigration(Brand $brand, string $table): void
39    {
40        $this->table = $table;
41        $this->brand = $brand;
42        $this->instruct();
43        $this->up();
44    }
45
46    /**
47     * Instruct.
48     *
49     * @return array
50     * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
51     */
52    public function instruct(): array
53    {
54        //phpcs:enable
55        $this->initFile();
56        $cleanDegeu = function (string $str) {
57            return html_entity_decode(
58                trim(str_replace('\\', '', ucwords(stripcslashes(strtolower($str))))),
59                ENT_QUOTES | ENT_HTML5,
60                'UTF-8'
61            );
62        };
63        return [
64            'Users' => [
65                'table' => $this->table,
66                'model' => Customer::class,
67                'comment' => sprintf('Migrating %s customers', $this->brand->name),
68                'bind' => [
69                    'email' => [
70                        'apply' => function ($v, $model) use ($cleanDegeu) {
71                            $v                      = \mb_convert_encoding($v, "UTF-8");
72                            $v                      = explode('#|#', $v);
73                            $oldId                  = $v[0];
74                            $first_name             = $cleanDegeu($v[1]);
75                            $last_name              = $cleanDegeu($v[2]);
76                            $birth_date             = $v[3];
77                            $address                = $cleanDegeu($v[4]);
78                            $address2               = $cleanDegeu($v[5]);
79                            $city                   = $cleanDegeu($v[6]);
80                            $postcode               = $v[7];
81                            $user_inscritNewsletter = $v[8];
82                            $email                  = strtolower($v[9]);
83                            $pwd                    = $v[10];
84                            $created_at             = $v[11];
85                            $updated_at             = $v[12];
86                            $last_cnx               = $v[13];
87
88                            // * Just save Single optin.
89                            if (empty($first_name) && empty($last_name)) {
90                                $sglOptin           = SingleOptin::query()->where('email', strtolower($email))
91                                    ->whereHas('brand', function (Builder $q) {
92                                        $q->where('id', $this->brand->id);
93                                    })->firstOrNew();
94                                $sglOptin->email    = strtolower($email);
95                                $sglOptin->brand_id = $this->brand->id;
96                                $sglOptin->save();
97                            } else {
98                                // * Validate Data.
99                                $d = [
100                                    'first_name' => $first_name,
101                                    'last_name' => $last_name,
102                                    'address' => $address,
103                                    'address2' => $address2,
104                                    'city' => $city,
105                                    'postcode' => $postcode,
106                                ];
107
108                                $validator = Validator::make($d, [
109                                    'first_name' => 'min:2',
110                                    'last_name' => 'min:2'
111                                ]);
112
113                                if ($validator->fails()) {
114                                    throw new AbortRowImportException();
115                                }
116
117                                $rules = [
118                                    'address' => 'string|min:2',
119                                    'address2' => 'string|min:2',
120                                    'city' => 'string|min:2',
121                                    'postcode' => 'digits_between:4,5'
122                                ];
123
124                                $validate = function (array $data, array $rule) {
125                                    return Validator::make($data, $rule)->fails();
126                                };
127
128                                foreach ($rules as $key => $rule) {
129                                    if ($validate([$key => $d[$key]], [$key => $rule])) {
130                                        ${$key} = null;
131                                    }
132                                }
133
134                                $model             = Customer::where('email', $email)->firstOrNew();
135                                $model->email      = $email;
136                                $model->created_at = $created_at;
137                                $model->updated_at = $updated_at;
138                                $model->save();
139
140                                // * Filtre birthdate puisque c'est du sale dans la vielle crm.
141                                try {
142                                    $expl = explode('-', $birth_date);
143                                    $d    = Carbon::parse($birth_date);
144                                    if (
145                                        $d->toDateString() === sprintf(
146                                            '%s-%s-%s',
147                                            $expl[2] ?? '',
148                                            $expl[1] ?? '',
149                                            $expl[0] ?? ''
150                                        )
151                                    ) {
152                                        $birth_date = $d->toDateString();
153                                    }
154                                } catch (InvalidFormatException $e) {
155                                    $birth_date = '';
156                                }
157
158
159                                if ($birth_date === '0000-00-00') {
160                                    $birth_date = '';
161                                }
162
163                                $city     = !empty($city) ? $city : null;
164                                $postcode = !empty($postcode) ? $postcode : null;
165                                $address  = !empty($address) ? $address : null;
166                                $address2 = !empty($address2) ? $address2 : null;
167
168                                $cInfo              = new CustomerInfo();
169                                $cInfo->hashcode    = Str::uuid();
170                                $cInfo->firstname   = ucfirst(strtolower($first_name));
171                                $cInfo->lastname    = ucfirst(strtolower($last_name));
172                                $cInfo->birth_date  = !strlen($birth_date) ? null : $birth_date;
173                                $cInfo->address     = $address;
174                                $cInfo->address2    = $address2;
175                                $cInfo->city        = $city;
176                                $cInfo->postcode    = $postcode;
177                                $cInfo->opt_in      = $user_inscritNewsletter ? true : false;
178                                $cInfo->brand_id    = $this->brand->id;
179                                $cInfo->customer_id = $model->id;
180                                $cInfo->created_at  = $created_at;
181                                $cInfo->updated_at  = $updated_at;
182                                $cInfo->save();
183
184                                $cLogin                  = new CustomerLogin();
185                                $cLogin->password        = Hash::make(trim($pwd));
186                                $cLogin->brand_id        = $this->brand->id;
187                                $cLogin->customer_id     = $model->id;
188                                $cLogin->status          = true;
189                                $cLogin->created_at      = $created_at;
190                                $cLogin->updated_at      = $updated_at;
191                                $cLogin->last_connection = $last_cnx;
192                                $cLogin->save();
193
194                                $this->write(
195                                    [
196                                        'new' => $model->id,
197                                        'old' => $oldId,
198                                        'hashcode' => $cInfo->hashcode,
199                                        'created_at' => $created_at,
200                                        'updated_at' => $updated_at
201                                    ]
202                                );
203                            } //end if
204                            throw new AbortRowImportException();
205                        },
206                        'name' => [
207                            'user_id',
208                            'firstname',
209                            'lastname',
210                            'birthdate',
211                            'address',
212                            'address2',
213                            'city',
214                            'postcode',
215                            'optin',
216                            'email',
217                            'pwd',
218                            'created_at',
219                            'updated_at',
220                            'last_cnx'
221                        ]
222                    ],
223                ]
224            ],
225        ];
226    }
227
228    /**
229     * Create the migration file.
230     *
231     * @return void
232     */
233    public function initFile(): void
234    {
235        File::put(storage_path('crm_migration.csv'), sprintf('new;old;hashcode;created_at;updated_at') . PHP_EOL);
236    }
237
238    /**
239     * Write date in the migration file.
240     *
241     * @param array $data
242     * @return void
243     */
244    public function write(array $data): void
245    {
246        foreach ($data as &$d) {
247            $d = sprintf('"%s"', $d);
248        }
249        File::append(storage_path('crm_migration.csv'), implode(';', $data) . PHP_EOL);
250    }
251}