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