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