Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ProcessShortcutAsyncJob | |
0.00% |
0 / 49 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| backoff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| created | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| updateUserInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| parseFields | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| updateDailyUserInfo | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
6 | |||
| parseDailyFields | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Http\Models\Shortcut; |
| 6 | use App\Traits\UsageTrait; |
| 7 | use Illuminate\Bus\Queueable; |
| 8 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 9 | use Illuminate\Foundation\Bus\Dispatchable; |
| 10 | use Illuminate\Queue\InteractsWithQueue; |
| 11 | use Illuminate\Queue\SerializesModels; |
| 12 | use App\Http\Models\UserInfo; |
| 13 | use App\Http\Models\FlyMsgUserDailyUsage; |
| 14 | use MongoDB\BSON\UTCDateTime; |
| 15 | |
| 16 | class ProcessShortcutAsyncJob implements ShouldQueue |
| 17 | { |
| 18 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UsageTrait; |
| 19 | |
| 20 | public $tries = 5; |
| 21 | |
| 22 | public function __construct( |
| 23 | public Shortcut $shortcut, |
| 24 | public string $action |
| 25 | ) {} |
| 26 | |
| 27 | public function handle(): void |
| 28 | { |
| 29 | if ($this->action === 'created') { |
| 30 | $this->created($this->shortcut); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public function backoff() |
| 35 | { |
| 36 | return [10, 30, 60, 110, 300]; |
| 37 | } |
| 38 | |
| 39 | private function created(Shortcut $shortcut): void |
| 40 | { |
| 41 | $parsedFields = $this->parseFields($shortcut); |
| 42 | $this->updateUserInfo($parsedFields, $shortcut); |
| 43 | |
| 44 | $parseDailyFields = $this->parseDailyFields($shortcut); |
| 45 | $this->updateDailyUserInfo($parseDailyFields, $shortcut); |
| 46 | } |
| 47 | |
| 48 | private function updateUserInfo(array $data, Shortcut $shortcut) |
| 49 | { |
| 50 | UserInfo::where('user_id', $shortcut->user_id)->update($data); |
| 51 | } |
| 52 | |
| 53 | private function parseFields(Shortcut $shortcut): array |
| 54 | { |
| 55 | if ($shortcut->user_defined) { |
| 56 | return [ |
| 57 | 'number_of_flycuts_created_last_date' => new UTCDateTime($shortcut->created_at->timestamp * 1000), |
| 58 | 'number_of_flycuts_created_count' => Shortcut::where('user_id', $shortcut->user_id)->where('user_defined', true)->count(), |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | return [ |
| 63 | 'number_of_flyplates_in_flycuts_last_date' => new UTCDateTime($shortcut->created_at->timestamp * 1000), |
| 64 | 'number_of_flyplates_in_flycuts_count' => Shortcut::where('user_id', $shortcut->user_id)->where('user_defined', false)->count(), |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | private function updateDailyUserInfo(array $data, Shortcut $shortcut) |
| 69 | { |
| 70 | $date = $shortcut->created_at; |
| 71 | $year = $date->year; |
| 72 | $month = $date->month; |
| 73 | $day = $date->day; |
| 74 | $findByDate = FlyMsgUserDailyUsage::where('user_id', $shortcut->user_id)->where('year', $year)->where('month', $month)->where('day', $day)->first(); |
| 75 | |
| 76 | if ($findByDate) { |
| 77 | $findByDate->update($data); |
| 78 | $findByDate->save(); |
| 79 | } else { |
| 80 | FlyMsgUserDailyUsage::GenerateNewRecord( |
| 81 | $year, |
| 82 | $month, |
| 83 | $day, |
| 84 | $shortcut->user_id, |
| 85 | $data['time_saved'], |
| 86 | $data['cost_savings'], |
| 87 | $data['characters_typed'], |
| 88 | $data['flycuts_created'], |
| 89 | $data['flyplates_added'], |
| 90 | $data['flyengage_count'], |
| 91 | $data['sentence_rewrite_count'], |
| 92 | $data['paragraph_rewrite_count'], |
| 93 | $data['flypost_count'], |
| 94 | $data['flycut_count'], |
| 95 | $data['fly_grammar_actions'] ?? 0, |
| 96 | $data['fly_grammar_accepted'] ?? 0, |
| 97 | $data['fly_grammar_autocorrect'] ?? 0, |
| 98 | $data['fly_grammar_autocomplete'] ?? 0, |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private function parseDailyFields(Shortcut $shortcut): array |
| 104 | { |
| 105 | $user_id = $shortcut->user_id; |
| 106 | $date = $shortcut->created_at; |
| 107 | |
| 108 | return $this->getUsage($user_id, $date); |
| 109 | } |
| 110 | } |