Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
Shortcut | |
0.00% |
0 / 14 |
|
0.00% |
0 / 13 |
182 | |
0.00% |
0 / 1 |
getNumberOfTimesUsedAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTimeSavedAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCostSavedAttribute | |
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 | |||
shortcutVersions | |
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 | |||
getVersionCountsAttribute | |
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 | use App\Observers\ShortcutObserver; |
8 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
9 | use Illuminate\Database\Eloquent\Attributes\ObservedBy; |
10 | use Illuminate\Notifications\Notifiable; |
11 | |
12 | #[ObservedBy([ShortcutObserver::class])] |
13 | class Shortcut extends Moloquent |
14 | { |
15 | use HasFactory, Notifiable; |
16 | |
17 | protected $table = 'shortcuts'; |
18 | |
19 | protected $fillable = [ |
20 | 'title', |
21 | 'shortcut', |
22 | 'first_line', |
23 | 'text', |
24 | 'html', |
25 | 'text_backup', |
26 | 'html_backup', |
27 | 'version', |
28 | 'category_id', |
29 | 'media_sizes', |
30 | 'uploads', |
31 | 'type', |
32 | 'template_id', |
33 | 'template_version', |
34 | 'template_category_id', |
35 | 'user_defined', |
36 | 'user_id', |
37 | 'sub_categories_lv1_id', |
38 | 'sub_categories_lv2_id', |
39 | 'rollback_counts', |
40 | 'version_counts', |
41 | 'charactors_count', |
42 | 'words_count', |
43 | 'html_count', |
44 | 'shareable', |
45 | 'fly_share_ids', |
46 | 'reference_shares_shortcut_id', |
47 | 'flycutUsage_count', |
48 | 'flycutUsage_time_saved', |
49 | 'flycutUsage_cost_saved', |
50 | ]; |
51 | |
52 | /** |
53 | * The attributes that should be hidden for arrays. |
54 | * |
55 | * @var array |
56 | */ |
57 | protected $hidden = ['fly_share_ids']; |
58 | |
59 | protected $cast = [ |
60 | 'uploads' => 'array', |
61 | ]; |
62 | |
63 | /** |
64 | * The accessors to append to the model's array form. |
65 | * |
66 | * @var array |
67 | */ |
68 | protected $appends = [ |
69 | 'version_counts', |
70 | 'number_of_times_used', |
71 | 'time_saved', |
72 | ]; |
73 | |
74 | /** |
75 | * Get the number_of_times_used atrribute for the shortcut. |
76 | * |
77 | * @return int |
78 | */ |
79 | public function getNumberOfTimesUsedAttribute() |
80 | { |
81 | return $this->flycutUsage_count ?? 0; |
82 | } |
83 | |
84 | /** |
85 | * Get the time_saved atrribute for the shortcut. |
86 | * |
87 | * @return float |
88 | */ |
89 | public function getTimeSavedAttribute() |
90 | { |
91 | return $this->flycutUsage_time_saved ?? 0; |
92 | } |
93 | |
94 | public function getCostSavedAttribute() |
95 | { |
96 | return $this->flycutUsage_cost_saved ?? 0; |
97 | } |
98 | |
99 | /** |
100 | * The "booting" method of the model. |
101 | */ |
102 | protected static function boot(): void |
103 | { |
104 | parent::boot(); |
105 | // Return only shortcuts belonging to the logged in user |
106 | static::addGlobalScope(new UserScope); |
107 | } |
108 | |
109 | public function category() |
110 | { |
111 | return $this->belongsTo(ShortcutCategory::class, 'category_id', '_id'); |
112 | } |
113 | |
114 | public function shortcutVersions() |
115 | { |
116 | return $this->hasMany(ShortcutVersion::class, 'shortcut_id'); |
117 | } |
118 | |
119 | public function SubCategoryLv1() |
120 | { |
121 | return $this->belongsTo(ShortcutSubCategoryLv1::class, 'sub_categories_lv1_id'); |
122 | } |
123 | |
124 | public function SubCategoryLv2() |
125 | { |
126 | return $this->belongsTo(ShortcutSubCategoryLv2::class, 'sub_categories_lv2_id'); |
127 | } |
128 | |
129 | public function template() |
130 | { |
131 | return $this->belongsTo(Template::class); |
132 | } |
133 | |
134 | /** |
135 | * The flyshares that belong to the shortcut. |
136 | * |
137 | * @deprecated No longer used by internal code. |
138 | */ |
139 | public function flyshares() |
140 | { |
141 | return $this->belongsToMany(FlyShare::class, null, 'shortcut_ids', 'fly_share_ids'); |
142 | } |
143 | |
144 | /** |
145 | * Get the user that owns the Shortcut |
146 | * |
147 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
148 | */ |
149 | public function user() |
150 | { |
151 | return $this->belongsTo(User::class); |
152 | } |
153 | |
154 | //TODO MA - Not sure about this yet |
155 | // public function getVersionAttribute() |
156 | // { |
157 | // return $this->shortcutVersions()->count(); |
158 | // } |
159 | |
160 | /** |
161 | * Added ->withoutGlobalScope(UserScope::class) because it's affecting the |
162 | * code anywhere the user context is not available e.g queued listeners |
163 | * |
164 | */ |
165 | public function getVersionCountsAttribute() |
166 | { |
167 | return $this->shortcutVersions()->withoutGlobalScope(UserScope::class)->count(); |
168 | } |
169 | |
170 | /** |
171 | * Get the flycut usage records for the shortcut. |
172 | */ |
173 | public function flycutUsage() |
174 | { |
175 | return $this->hasMany(FlyCutUsage::class); |
176 | } |
177 | } |