Test Failed
Push — dev5 ( ba6069...125e24 )
by Ron
09:51
created

DatabaseCheck::file_link_files()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 20
ccs 0
cts 10
cp 0
crap 12
rs 9.9332
1
<?php
2
3
namespace App\Domains\Maintenance;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\DB;
7
8
9
use App\User;
10
use App\Files;
11
use App\TechTips;
12
use App\FileLinks;
13
use App\Customers;
14
use App\UserLogins;
15
use App\SystemTypes;
16
use App\TechTipFavs;
17
use App\CustomerFavs;
18
use App\UserRoleType;
19
use App\TechTipFiles;
20
use App\TechTipTypes;
21
use App\FileLinkFiles;
22
use App\CustomerFiles;
23
use App\CustomerNotes;
24
use App\TechTipSystems;
25
use App\TechTipComments;
26
use App\CustomerSystems;
27
use App\CustomerContacts;
28
use App\PhoneNumberTypes;
29
use App\SystemCategories;
30
use App\SystemDataFields;
31
use App\CustomerFileTypes;
32
use App\CustomerSystemData;
33
use App\UserRolePermissions;
34
use App\SystemDataFieldTypes;
35
use App\CustomerContactPhones;
36
use App\UserRolePermissionTypes;
37
38
class DatabaseCheck
39
{
40
    protected $fix;
41
42
    public function __construct($fix = false)
43
    {
44
        $this->fix = $fix;
45
    }
46
47
    public function execute($table)
48
    {
49
        return $this->$table();
50
    }
51
52
    protected function checkForeign($table, $fTable, $fKey)
53
    {
54
        $valid = true;
55
        $list = DB::select('SELECT '.$fKey.' FROM '.$table.' GROUP BY '.$fKey);
56
        foreach($list as $l)
57
        {
58
            if($l->$fKey != null)
59
            {
60
                $v = DB::select('SELECT * FROM '.$fTable.' WHERE '.$fKey.' = '.$l->$fKey);
61
                if(!$v)
0 ignored issues
show
Bug Best Practice introduced by
The expression $v of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
                {
63
                    $valid = false;
64
                    if($this->fix)
65
                    {
66
                        $data = DB::select('SELECT * FROM '.$table.' WHERE '.$fKey.' = '.$l->$fKey);
67
                        Log::notice('DBCheck - '.$table.' Foreign key failed.  Deleting data - ', array($data));
68
                        DB::delete('delete from '.$table.' where '.$fKey.' = '.$l->$fKey);
69
                    }
70
                }
71
            }
72
        }
73
74
        return $valid;
75
    }
76
77
    //  users table contains the user_role foreign key
78
    protected function users()
79
    {
80
        return $this->checkForeign('users', 'user_role_types', 'role_id');
81
    }
82
83
    //  user_role_permissions table has the role_id and perm_type_id foreign keys
84
    protected function user_role_permissions()
85
    {
86
        $valid = true;
87
        $table = 'user_role_permissions';
88
89
        $keyArr = [
90
            'user_role_types'            => 'role_id',
91
            'user_role_permission_types' => 'perm_type_id',
92
        ];
93
94
        foreach($keyArr as $fTable => $key)
95
        {
96
            if(!$this->checkForeign($table, $fTable, $key))
97
            {
98
                $valid = false;
99
            }
100
        }
101
102
        return $valid;
103
    }
104
105
    //  user_logins table has the user_id foreign key
106
    protected function user_logins()
107
    {
108
        return $this->checkForeign('user_logins', 'users', 'user_id');
109
    }
110
111
    //  tech_tips table has user_id, updated_id and tip_type_id foreign keys
112
    protected function tech_tips()
113
    {
114
        $valid = true;
115
        $table = 'tech_tips';
116
117
        $keyArr = [
118
            'users' => 'user_id',
119
            // 'updated_id' => 'user_id'
120
            //  TODO - check updated_id key
121
            'tech_tip_types' => 'tip_type_id',
122
        ];
123
124
        foreach($keyArr as $fTable => $key)
125
        {
126
            if(!$this->checkForeign($table, $fTable, $key))
127
            {
128
                $valid = false;
129
            }
130
        }
131
132
        return $valid;
133
    }
134
135
    //  tech_tip_systems table has tip_id and sys_id foreign keys
136
    protected function tech_tip_systems()
137
    {
138
        $valid = true;
139
        $table = 'tech_tip_systems';
140
141
        $keyArr = [
142
            'tech_tips'    => 'tip_id',
143
            'system_types' => 'sys_id',
144
        ];
145
146
        foreach($keyArr as $fTable => $key)
147
        {
148
            if(!$this->checkForeign($table, $fTable, $key))
149
            {
150
                $valid = false;
151
            }
152
        }
153
154
        return $valid;
155
    }
156
157
    //  tech_tip_files table has tip_id and file_id foreign keys
158
    protected function tech_tip_files()
159
    {
160
        $valid = true;
161
        $table = 'tech_tip_files';
162
163
        $keyArr = [
164
            'tech_tips' => 'tip_id',
165
            'files'     => 'file_id',
166
        ];
167
168
        foreach($keyArr as $fTable => $key)
169
        {
170
            if(!$this->checkForeign($table, $fTable, $key))
171
            {
172
                $valid = false;
173
            }
174
        }
175
176
        return $valid;
177
    }
178
179
    //  tech_tip_favs table has the user_id and tip_id foreign keys
180
    protected function tech_tip_favs()
181
    {
182
        $valid = true;
183
        $table = 'tech_tip_favs';
184
185
        $keyArr = [
186
            'users'     => 'user_id',
187
            'tech_tips' => 'tip_id',
188
        ];
189
190
        foreach($keyArr as $fTable => $key)
191
        {
192
            if(!$this->checkForeign($table, $fTable, $key))
193
            {
194
                $valid = false;
195
            }
196
        }
197
198
        return $valid;
199
    }
200
201
    //  tech_tip_comments table has the tip_id and user_id foreign keys
202
    protected function tech_tip_comments()
203
    {
204
        $valid = true;
205
        $table = 'tech_tip_comments';
206
207
        $keyArr = [
208
            'tech_tips' => 'tip_id',
209
            'users'     => 'user_id',
210
        ];
211
212
        foreach($keyArr as $fTable => $key)
213
        {
214
            if(!$this->checkForeign($table, $fTable, $key))
215
            {
216
                $valid = false;
217
            }
218
        }
219
220
        return $valid;
221
    }
222
223
    //  system_types table has cat_id foreign key
224
    protected function system_types()
225
    {
226
        return $this->checkForeign('system_types', 'system_categories', 'cat_id');
227
    }
228
229
    //  system_data_fields table has sys_id and data_type_id foreign keys
230
    protected function system_data_fields()
231
    {
232
        $valid = true;
233
        $table = 'system_data_fields';
234
235
        $keyArr = [
236
            'system_types'            => 'sys_id',
237
            'system_data_field_types' => 'data_type_id',
238
        ];
239
240
        foreach($keyArr as $fTable => $key)
241
        {
242
            if(!$this->checkForeign($table, $fTable, $key))
243
            {
244
                $valid = false;
245
            }
246
        }
247
248
        return $valid;
249
    }
250
251
252
    //  file_links table has user_id and cust_id foreign keys
253
    protected function file_links()
254
    {
255
        $valid = true;
256
        $table = 'file_links';
257
258
        $keyArr = [
259
            'users'     => 'user_id',
260
            'customers' => 'cust_id',
261
        ];
262
263
        foreach($keyArr as $fTable => $key)
264
        {
265
            if(!$this->checkForeign($table, $fTable, $key))
266
            {
267
                $valid = false;
268
            }
269
        }
270
271
        return $valid;
272
    }
273
274
    //  file_link_files table has link_id, file_id, and user_id foreign keys
275
    protected function file_link_files()
276
    {
277
        $valid = true;
278
        $table = 'file_link_files';
279
280
        $keyArr = [
281
            'file_links' => 'link_id',
282
            'files'      => 'file_id',
283
            'users'      => 'user_id',
284
        ];
285
286
        foreach($keyArr as $fTable => $key)
287
        {
288
            if(!$this->checkForeign($table, $fTable, $key))
289
            {
290
                $valid = false;
291
            }
292
        }
293
294
        return $valid;
295
    }
296
297
    //  customer_systems table has cust_id and sys_id foreign keys
298
    protected function customer_systems()
299
    {
300
        $valid = true;
301
        $table = 'customer_systems';
302
303
        $keyArr = [
304
            'customers'    => 'cust_id',
305
            'system_types' => 'sys_id',
306
        ];
307
308
        foreach($keyArr as $fTable => $key)
309
        {
310
            if(!$this->checkForeign($table, $fTable, $key))
311
            {
312
                $valid = false;
313
            }
314
        }
315
316
        return $valid;
317
    }
318
319
    //  The customer_system_data table has the cust_sys_id and field_id foreign keys
320
    protected function customer_system_data()
321
    {
322
        $valid = true;
323
        $table = 'customer_system_data';
324
325
        $keyArr = [
326
            'customer_systems'   => 'cust_sys_id',
327
            'system_data_fields' => 'field_id',
328
        ];
329
330
        foreach($keyArr as $fTable => $key)
331
        {
332
            if(!$this->checkForeign($table, $fTable, $key))
333
            {
334
                $valid = false;
335
            }
336
        }
337
338
        return $valid;
339
    }
340
341
    //  customer_notes table has cust_id and user_id foreign keys
342
    protected function customer_notes()
343
    {
344
        $valid = true;
345
        $table = 'customer_notes';
346
347
        $keyArr = [
348
            'customers' => 'cust_id',
349
            'users'     => 'user_id',
350
        ];
351
352
        foreach($keyArr as $fTable => $key)
353
        {
354
            if(!$this->checkForeign($table, $fTable, $key))
355
            {
356
                $valid = false;
357
            }
358
        }
359
360
        return $valid;
361
    }
362
363
    //  customer_files table has file_id, file_type_id, cust_id, and user_id foreign keys
364
    protected function customer_files()
365
    {
366
        $valid = true;
367
        $table = 'customer_files';
368
369
        $keyArr = [
370
            'files'               => 'file_id',
371
            'customer_file_types' => 'file_type_id',
372
            'customers'           => 'cust_id',
373
            'users'               => 'user_id',
374
        ];
375
376
        foreach($keyArr as $fTable => $key)
377
        {
378
            if(!$this->checkForeign($table, $fTable, $key))
379
            {
380
                $valid = false;
381
            }
382
        }
383
384
        return $valid;
385
    }
386
387
    //  customer_favs table has user_id and cust_id foreign keys
388
    protected function customer_favs()
389
    {
390
        $valid = true;
391
        $table = 'customer_favs';
392
393
        $keyArr = [
394
            'users'     => 'user_id',
395
            'customers' => 'cust_id',
396
        ];
397
398
        foreach($keyArr as $fTable => $key)
399
        {
400
            if(!$this->checkForeign($table, $fTable, $key))
401
            {
402
                $valid = false;
403
            }
404
        }
405
406
        return $valid;
407
    }
408
409
    //  customer_contacts table has cust_id foreign key
410
    protected function customer_contacts()
411
    {
412
        return $this->checkForeign('customer_contacts', 'customers', 'cust_id');
413
    }
414
415
    //  customer_contact_phones table has cont_id and phone_type_id table
416
    protected function customer_contact_phones()
417
    {
418
        $valid = true;
419
        $table = 'customer_contact_phones';
420
421
        $keyArr = [
422
            'customer_contacts'  => 'cont_id',
423
            'phone_number_types' => 'phone_type_id',
424
        ];
425
426
        foreach($keyArr as $fTable => $key)
427
        {
428
            if(!$this->checkForeign($table, $fTable, $key))
429
            {
430
                $valid = false;
431
            }
432
        }
433
434
        return $valid;
435
    }
436
}
437