Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ProcessSettingAsyncJob | |
0.00% |
0 / 25 |
|
0.00% |
0 / 8 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| backoff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| created | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| updated | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| updateUserInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateUpdatedFields | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| parseFields | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use Illuminate\Bus\Queueable; |
| 6 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 7 | use Illuminate\Foundation\Bus\Dispatchable; |
| 8 | use Illuminate\Queue\InteractsWithQueue; |
| 9 | use Illuminate\Queue\SerializesModels; |
| 10 | use App\Http\Models\Setting; |
| 11 | use App\Http\Models\UserInfo; |
| 12 | use App\Traits\ObjectMapper; |
| 13 | |
| 14 | class ProcessSettingAsyncJob implements ShouldQueue |
| 15 | { |
| 16 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ObjectMapper; |
| 17 | |
| 18 | public $tries = 5; |
| 19 | |
| 20 | public function __construct( |
| 21 | public Setting $setting, |
| 22 | public string $action |
| 23 | ) {} |
| 24 | |
| 25 | public function handle(): void |
| 26 | { |
| 27 | if ($this->action === 'created') { |
| 28 | $this->created($this->setting); |
| 29 | } elseif ($this->action === 'updated') { |
| 30 | $this->updated($this->setting); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public function backoff() |
| 35 | { |
| 36 | return [10, 30, 60, 120, 300]; |
| 37 | } |
| 38 | |
| 39 | private function created(Setting $setting): void |
| 40 | { |
| 41 | $created = $setting->toArray(); |
| 42 | |
| 43 | $parsedFields = $this->parseFields($created); |
| 44 | |
| 45 | $this->updateUserInfo($parsedFields, $setting); |
| 46 | } |
| 47 | |
| 48 | private function updated(Setting $setting): void |
| 49 | { |
| 50 | $updated = $setting->getDirty(); |
| 51 | |
| 52 | if (!empty($updated)) { |
| 53 | $fields = $this->validateUpdatedFields($updated); |
| 54 | |
| 55 | if (!empty($fields)) { |
| 56 | $parsedFields = $this->parseFields($updated); |
| 57 | |
| 58 | $this->updateUserInfo($parsedFields, $setting); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private function updateUserInfo(array $data, Setting $setting) |
| 64 | { |
| 65 | UserInfo::where('user_id', $setting->user_id)->update($data); |
| 66 | } |
| 67 | |
| 68 | private function validateUpdatedFields(array $updated): array |
| 69 | { |
| 70 | $hubspotFields = [ |
| 71 | 'words_per_minute', |
| 72 | ]; |
| 73 | |
| 74 | return array_filter($updated, fn($key) => in_array($key, $hubspotFields), ARRAY_FILTER_USE_KEY); |
| 75 | } |
| 76 | |
| 77 | private function parseFields(array $fields): array |
| 78 | { |
| 79 | $map = [ |
| 80 | 'words_per_minute' => ['rename' => 'typed_words_per_minute'], |
| 81 | 'fly_grammar_default_language' => [] |
| 82 | ]; |
| 83 | |
| 84 | return $this->mapObject($fields, $map); |
| 85 | } |
| 86 | } |