Test Setup Failed
Push — master ( 93675a...ba3342 )
by Mohamed
14:07 queued 12s
created

Unique_user_id   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 19
dl 0
loc 65
rs 10
c 3
b 1
f 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateOrInsertRecord() 0 20 4
A __construct() 0 3 1
1
<?php
2
3
namespace App\Models;
4
5
use Lsf\UniqueUid\UniqueUid;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Database\Eloquent\Model;
8
9
class Unique_user_id extends Model
10
{
11
12
    /**
13
     * The database table used by the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'unique_user_id';
18
19
    /**
20
     * Attributes that should be mass-assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = ['unique_id', 'security_user_id'];
25
26
    /**
27
     * The attributes excluded from the model's JSON form.
28
     *
29
     * @var array
30
     */
31
    protected $hidden = [];
32
33
    /**
34
     * The attributes that should be casted to native types.
35
     *
36
     * @var array
37
     */
38
    protected $casts = [];
39
40
    /**
41
     * The attributes that should be mutated to dates.
42
     *
43
     * @var array
44
     */
45
    protected $dates = [];
46
47
    public function __construct()
48
    {
49
        $this->uniqueUserId = new UniqueUid();
0 ignored issues
show
Bug introduced by
The property uniqueUserId does not seem to exist on App\Models\Unique_user_id. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
50
    }
51
52
53
54
    public  function updateOrInsertRecord($user)
55
    {
56
       try {
57
            // regenerate unique id if it's not available
58
        $uniqueId =  $this->uniqueUserId::isValidUniqueId($user['openemis_no']) ? $this->uniqueUserId::getUniqueAlphanumeric() : $user['openemis_no'];
59
60
        //check if the user's entry exits ?
61
        $exists = Unique_user_id::where([
62
            'security_user_id' => $user['id'],
63
            'unique_id' =>  $uniqueId
64
        ])->exists();
65
        if (!$exists) {
66
            // try to feed unique user id
67
            Unique_user_id::insert([
68
                'security_user_id' => $user['id'],
69
                'unique_id' =>  $uniqueId
70
            ]);
71
        }
72
       } catch (\Exception $th) {
73
           Log::error($th->getMessage());
74
       }
75
    }
76
}
77