Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
ShortcutWithUsage | |
0.00% |
0 / 12 |
|
0.00% |
0 / 11 |
132 | |
0.00% |
0 / 1 |
getNumberOfTimesUsedAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTimeSavedAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTimeCreatedAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
boot | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
category | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
SubCategoryLv1 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
SubCategoryLv2 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
template | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flyshares | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
user | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flycutUsage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Http\Models; |
4 | |
5 | use App\Http\Models\Auth\User; |
6 | use App\Http\Scopes\UserScope; |
7 | |
8 | class ShortcutWithUsage extends Moloquent |
9 | { |
10 | protected $table = 'shortcuts'; |
11 | |
12 | protected $fillable = [ |
13 | 'title', |
14 | 'shortcut', |
15 | 'first_line', |
16 | 'text', |
17 | 'html', |
18 | 'version', |
19 | 'category_id', |
20 | 'media_sizes', |
21 | 'uploads', |
22 | 'type', |
23 | 'template_id', |
24 | 'template_version', |
25 | 'template_category_id', |
26 | 'user_defined', |
27 | 'user_id', |
28 | 'sub_categories_lv1_id', |
29 | 'sub_categories_lv2_id', |
30 | 'rollback_counts', |
31 | 'version_counts', |
32 | 'charactors_count', |
33 | 'words_count', |
34 | 'html_count', |
35 | 'shareable', |
36 | 'fly_share_ids', |
37 | 'reference_shares_shortcut_id', |
38 | 'flycutUsage_count', |
39 | ]; |
40 | |
41 | /** |
42 | * The attributes that should be hidden for arrays. |
43 | * |
44 | * @var array |
45 | */ |
46 | protected $hidden = ['fly_share_ids']; |
47 | |
48 | protected $cast = [ |
49 | 'uploads' => 'array', |
50 | ]; |
51 | |
52 | /** |
53 | * The accessors to append to the model's array form. |
54 | * |
55 | * @var array |
56 | */ |
57 | protected $appends = ['number_of_times_used', 'time_saved', 'time_created']; |
58 | |
59 | /** |
60 | * Get the number_of_times_used atrribute for the shortcut. |
61 | */ |
62 | public function getNumberOfTimesUsedAttribute(): int |
63 | { |
64 | return $this->flycutUsage()->count(); |
65 | } |
66 | |
67 | /** |
68 | * Get the time_saved atrribute for the shortcut. |
69 | */ |
70 | public function getTimeSavedAttribute(): float |
71 | { |
72 | return $this->flycutUsage()->sum('time_saved'); |
73 | } |
74 | |
75 | /** |
76 | * Get the time_created field which is the created_at field without mutation. |
77 | * |
78 | * @param int|float|string|DateTimeInterface $value |
79 | */ |
80 | public function getTimeCreatedAttribute($value): string |
81 | { |
82 | return $this->created_at; |
83 | } |
84 | |
85 | /** |
86 | * The "booting" method of the model. |
87 | */ |
88 | protected static function boot(): void |
89 | { |
90 | parent::boot(); |
91 | |
92 | //Return only shortcuts belonging to the logged in user |
93 | static::addGlobalScope(new UserScope); |
94 | } |
95 | |
96 | public function category() |
97 | { |
98 | return $this->belongsTo(ShortcutCategory::class, 'category_id'); |
99 | } |
100 | |
101 | public function SubCategoryLv1() |
102 | { |
103 | return $this->belongsTo(ShortcutSubCategoryLv1::class, 'sub_categories_lv1_id'); |
104 | } |
105 | |
106 | public function SubCategoryLv2() |
107 | { |
108 | return $this->belongsTo(ShortcutSubCategoryLv2::class, 'sub_categories_lv2_id'); |
109 | } |
110 | |
111 | public function template() |
112 | { |
113 | return $this->belongsTo(Template::class); |
114 | } |
115 | |
116 | /** |
117 | * The flyshares that belong to the shortcut. |
118 | * |
119 | * @deprecated No longer used by internal code. |
120 | */ |
121 | public function flyshares() |
122 | { |
123 | return $this->belongsToMany(FlyShare::class, null, 'shortcut_ids', 'fly_share_ids'); |
124 | } |
125 | |
126 | /** |
127 | * Get the user that owns the Shortcut |
128 | * |
129 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
130 | */ |
131 | public function user() |
132 | { |
133 | return $this->belongsTo(User::class); |
134 | } |
135 | |
136 | /** |
137 | * Get the flycut usage records for the shortcut. |
138 | */ |
139 | public function flycutUsage() |
140 | { |
141 | return $this->hasMany(FlyCutUsage::class); |
142 | } |
143 | } |