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