Device   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 237
Duplicated Lines 4.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 10
loc 237
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Models;
4
5
use App\Support\Traits\Eloquent\DeviceModelAttribute;
6
use Iatstuti\Database\Support\NullableFields;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Facades\Request;
9
10
class Device extends Model
11
{
12
    use DeviceModelAttribute, NullableFields;
13
14
    /**
15
     * The accessors to append to the model's array form.
16
     *
17
     * @var array
18
     */
19
    protected $appends = [
20
        'device_model', 'push_enabled',
21
    ];
22
23
    /**
24
     * The attributes that are mass assignable.
25
     *
26
     * @var array
27
     */
28
    protected $fillable = [
29
        'tdid', 'did', 'os', 'os_version', 'platform', 'model', 'name',
30
        'jailbroken', 'carrier', 'locale', 'network', 'ssid',
31
        'push_token', 'idfa', 'idfv', 'screen_width', 'screen_height',
32
        'screen_scale', 'timezone_gmt',
33
    ];
34
35
    /**
36
     * The attributes that should be mutated to dates.
37
     *
38
     * @var array
39
     */
40
    protected $dates = ['last_login_at'];
41
42
    /**
43
     * The attributes that should be cast to native types.
44
     *
45
     * @var array
46
     */
47
    protected $casts = [
48
        'jailbroken' => 'boolean',
49
        'screen_width' => 'integer',
50
        'screen_height' => 'integer',
51
        'screen_scale' => 'float',
52
        'timezone_gmt' => 'integer',
53
        'login_count' => 'integer',
54
    ];
55
56
    /**
57
     * The attributes that should be saved as null when empty.
58
     *
59
     * @var array
60
     */
61
    protected $nullable = [
62
        'did', 'model', 'name', 'carrier', 'locale', 'network', 'ssid',
63
        'push_token', 'idfa', 'idfv', 'last_login_ip', 'registered_ip',
64
    ];
65
66
    /**
67
     * The model's attributes.
68
     *
69
     * @var array
70
     */
71
    protected $attributes = [
72
        'jailbroken' => 0,
73
        'screen_width' => 0,
74
        'screen_height' => 0,
75
        'screen_scale' => 0,
76
        'timezone_gmt' => 0,
77
        'login_count' => 0,
78
    ];
79
80
    /**
81
     * The device that matches the current app client.
82
     *
83
     * @var static|bool
84
     */
85
    protected static $clientDevice = false;
86
87
    /**
88
     * Get the `push_enabled` attribute.
89
     *
90
     * @return bool
91
     */
92
    public function getPushEnabledAttribute()
93
    {
94
        return ! is_null($this->push_token);
95
    }
96
97
    /**
98
     * Find a Device instance.
99
     *
100
     * @param  string  $tdid
101
     * @return static|null
102
     */
103
    public static function findByTdid($tdid)
104
    {
105
        return ! empty($tdid) ? static::where('tdid', $tdid)->first() : null;
106
    }
107
108
    /**
109
     * Fetch device id for the given tdid.
110
     *
111
     * @param  string  $tdid
112
     * @return int|null
113
     */
114
    public static function fetchDeviceIdForTdid($tdid)
115
    {
116
        return static::where('tdid', $tdid)->value('id');
117
    }
118
119
    /**
120
     * Get the device for the current app client.
121
     *
122
     * @return static|null
123
     */
124
    public static function getClientDevice()
125
    {
126
        if (false === static::$clientDevice) {
127
            static::$clientDevice = static::findByTdid(app('client')->tdid);
128
        }
129
130
        return static::$clientDevice;
131
    }
132
133
    /**
134
     * Set the device for the current app client.
135
     *
136
     * @param  static  $device
137
     * @return static
138
     */
139
    public static function setClientDevice($device)
140
    {
141
        return static::$clientDevice = $device;
142
    }
143
144
    /**
145
     * Get the id for the client device.
146
     *
147
     * @return int|null
148
     */
149
    public static function getClientDeviceId()
150
    {
151
        if ($device = static::getClientDevice()) {
152
            return $device->id;
153
        }
154
    }
155
156
    /**
157
     * Update or create a Device instance.
158
     *
159
     * @param  array  $data
160
     * @return static
161
     *
162
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
163
     */
164
    public static function touchDevice(array $data)
165
    {
166
        $tdid = array_get($data, 'tdid');
167
168
        if (empty($tdid) || $tdid !== app('client')->tdid) {
169
            abort(403);
170
        }
171
172
        $device = static::firstOrNew(compact('tdid'));
173
174
        if (isset($data['screen_size']) && str_contains($data['screen_size'], 'x')) {
175
            $screenSize = explode('x', $data['screen_size']);
176
            if (count($screenSize) == 2) {
177
                $data['screen_width'] = (int) $screenSize[0];
178
                $data['screen_height'] = (int) $screenSize[1];
179
            }
180
        }
181
182
        $device->fill($data);
183
184
        $device->login_count++;
185
        $device->last_login_at = $device->freshTimestamp();
186
        $device->last_login_ip = Request::ip();
187
188
        if (! $device->exists) {
189
            $device->registered_ip = $device->last_login_ip;
190
        }
191
192
        $device->save();
193
194
        return static::setClientDevice($device);
195
    }
196
197
    /**
198
     * Update push token for the device.
199
     *
200
     * @param  string  $tdid
201
     * @param  string|null $push_token
202
     * @return bool
203
     */
204
    public static function updatePushTokenForTdid($tdid, $push_token = null)
205
    {
206
        return static::where('tdid', $tdid)->update(compact('push_token'));
207
    }
208
209
    /**
210
     * Get DeviceApp models.
211
     *
212
     * @return \Illuminate\Support\Collection
213
     */
214
    public function getDeviceApps()
215
    {
216
        return DeviceApp::findByDeviceId($this->id);
217
    }
218
219
    /**
220
     * Get UserDevice models.
221
     *
222
     * @param  bool  $withTrashed
223
     * @return mixed
224
     */
225
    public function getDeviceUsers($withTrashed = false)
226
    {
227
        return UserDevice::findByUserDevice(null, $this->id, $withTrashed);
228
    }
229
230
    /**
231
     * Get users to this device.
232
     *
233
     * @param  bool  $withTrashed
234
     * @return \Illuminate\Support\Collection
235
     */
236 View Code Duplication
    public function getUsers($withTrashed = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        return User::whereIn('id', function ($query) use ($withTrashed) {
239
            $query->select('user_id')->from('user_devices')->where('device_id', $this->id);
240
241
            if (! $withTrashed) {
242
                $query->where('deleted_at', null);
243
            }
244
        })->get();
245
    }
246
}
247