Passed
Push — laravel-8-support ( f89d92...92a2fc )
by Quentin
11:50
created

User::notifyWithCustomMarkdownTheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 15
ccs 7
cts 8
cp 0.875
crap 1.0019
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use A17\Twill\Models\Behaviors\HasMedias;
6
use A17\Twill\Models\Behaviors\HasPresenter;
7
use A17\Twill\Models\Behaviors\HasOauth;
8
use A17\Twill\Models\Enums\UserRole;
9
use A17\Twill\Models\Behaviors\IsTranslatable;
10
use A17\Twill\Notifications\Reset as ResetNotification;
11
use A17\Twill\Notifications\Welcome as WelcomeNotification;
12
use Illuminate\Auth\Authenticatable;
13
use Illuminate\Database\Eloquent\SoftDeletes;
14
use Illuminate\Foundation\Auth\Access\Authorizable;
15
use Illuminate\Foundation\Auth\User as AuthenticatableContract;
16
use Illuminate\Notifications\Notifiable;
17
use Illuminate\Support\Facades\Session;
18
use PragmaRX\Google2FAQRCode\Google2FA;
19
20
class User extends AuthenticatableContract
21
{
22
    use Authenticatable, Authorizable, HasMedias, Notifiable, HasPresenter, HasOauth, SoftDeletes, IsTranslatable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $email which is not provided by A17\Twill\Models\User.
Loading history...
Bug introduced by
The trait Illuminate\Auth\Authenticatable requires the property $password which is not provided by A17\Twill\Models\User.
Loading history...
Bug introduced by
The trait A17\Twill\Models\Behaviors\IsTranslatable requires the property $translatedAttributes which is not provided by A17\Twill\Models\User.
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasMedias requires some properties which are not provided by A17\Twill\Models\User: $medias, $role, $pivot, $metadatas, $uuid, $height, $lqip_data, $crop_h, $locale, $video, $crop, $crop_w, $width
Loading history...
introduced by
The trait A17\Twill\Models\Behaviors\HasOauth requires some properties which are not provided by A17\Twill\Models\User: $token, $avatar
Loading history...
23
24
    public $timestamps = true;
25
26
    protected $fillable = [
27
        'email',
28
        'name',
29
        'role',
30
        'published',
31
        'title',
32
        'description',
33
        'google_2fa_enabled',
34
        'google_2fa_secret',
35
        'language'
36
    ];
37
38
    protected $dates = [
39
        'deleted_at',
40
    ];
41
42
    protected $hidden = ['password', 'remember_token', 'google_2fa_secret'];
43
    public $checkboxes = ['published'];
44
45
    public $mediasParams = [
46
        'profile' => [
47
            'default' => [
48
                [
49
                    'name' => 'default',
50
                    'ratio' => 1,
51
                ],
52
            ],
53
        ],
54
    ];
55
56
    protected $casts = ['published' => 'boolean'];
57
58 69
    public function __construct(array $attributes = [])
59
    {
60 69
        $this->table = config('twill.users_table', 'twill_users');
61
62 69
        parent::__construct($attributes);
63 69
    }
64
65
    public function getTitleInBrowserAttribute()
66
    {
67
        return $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
68
    }
69
70
    public function getRoleValueAttribute()
71
    {
72
        if (!empty($this->role)) {
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
            if ($this->role == 'SUPERADMIN') {
74
                return "SUPERADMIN";
75
            }
76
77
            return UserRole::{$this->role}()->getValue();
78
        }
79
80
        return null;
81
    }
82
83
    public function getCanDeleteAttribute()
84
    {
85
        return auth('twill_users')->user()->id !== $this->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
86
    }
87
88 1
    public function scopePublished($query)
89
    {
90 1
        return $query->wherePublished(true);
91
    }
92
93 1
    public function scopeDraft($query)
94
    {
95 1
        return $query->wherePublished(false);
96
    }
97
98
    public function scopeOnlyTrashed($query)
99
    {
100
        return $query->whereNotNull('deleted_at');
101
    }
102
103 2
    public function setImpersonating($id)
104
    {
105 2
        Session::put('impersonate', $id);
106 2
    }
107
108 1
    public function stopImpersonating()
109
    {
110 1
        Session::forget('impersonate');
111 1
    }
112
113
    public function isImpersonating()
114
    {
115
        return Session::has('impersonate');
116
    }
117
118 2
    public function notifyWithCustomMarkdownTheme($instance)
119
    {
120 2
        $hostAppMailConfig = config('mail.markdown.paths');
121
122 2
        config([
123
            'mail.markdown.paths' => array_merge(
124 2
                [__DIR__ . '/../../views/emails'],
125
                $hostAppMailConfig
126
            ),
127
        ]);
128
129 2
        $this->notify($instance);
130
131 2
        config([
132 2
            'mail.markdown.paths' => $hostAppMailConfig,
133
        ]);
134
135 2
    }
136
137
    public function sendWelcomeNotification($token)
138
    {
139
        $this->notifyWithCustomMarkdownTheme(new WelcomeNotification($token));
140
    }
141
142 2
    public function sendPasswordResetNotification($token)
143
    {
144 2
        $this->notifyWithCustomMarkdownTheme(new ResetNotification($token));
145 2
    }
146
147 49
    public function isSuperAdmin()
148
    {
149 49
        return $this->role === 'SUPERADMIN';
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
150
    }
151
152 49
    public function isPublished()
153
    {
154 49
        return (bool) $this->published;
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
155
    }
156
157 2
    public function setGoogle2faSecretAttribute($secret)
158
    {
159 2
        $this->attributes['google_2fa_secret'] = filled($secret) ? \Crypt::encrypt($secret) : null;
160 2
    }
161
162 49
    public function getGoogle2faSecretAttribute($secret)
163
    {
164 49
        return filled($secret) ? \Crypt::decrypt($secret) : null;
165
    }
166
167 2
    public function generate2faSecretKey()
168
    {
169 2
        if (is_null($this->google_2fa_secret)) {
170 2
            $secret = (new Google2FA())->generateSecretKey();
171
172 2
            $this->google_2fa_secret = $secret;
0 ignored issues
show
Bug introduced by
The property google_2fa_secret does not seem to exist on A17\Twill\Models\User. 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...
173
174 2
            $this->save();
175
        }
176 2
    }
177
178 1
    public function get2faQrCode()
179
    {
180 1
        return (new Google2FA())->getQRCodeInline(
181 1
            config('app.name'),
182 1
            $this->email,
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $this->email can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $holder of PragmaRX\Google2FAQRCode...e2FA::getQRCodeInline() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
            /** @scrutinizer ignore-type */ $this->email,
Loading history...
183 1
            $this->google_2fa_secret,
184 1
            200
185
        );
186
    }
187
}
188