Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
ProcessCompanyAsyncJob | |
0.00% |
0 / 25 |
|
0.00% |
0 / 8 |
210 | |
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 | |||
deleted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
updated | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
updateUserInfo | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
validateUpdatedFields | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
parseFields | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Jobs; |
4 | |
5 | use App\Http\Models\Admin\Company; |
6 | use Illuminate\Bus\Queueable; |
7 | use Illuminate\Contracts\Queue\ShouldQueue; |
8 | use Illuminate\Foundation\Bus\Dispatchable; |
9 | use Illuminate\Queue\InteractsWithQueue; |
10 | use Illuminate\Queue\SerializesModels; |
11 | use App\Http\Models\UserInfo; |
12 | use App\Traits\ObjectMapper; |
13 | |
14 | class ProcessCompanyAsyncJob implements ShouldQueue |
15 | { |
16 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ObjectMapper; |
17 | |
18 | public $tries = 5; |
19 | |
20 | public function __construct( |
21 | public Company $company, |
22 | public string $action |
23 | ) {} |
24 | |
25 | public function handle(): void |
26 | { |
27 | if ($this->action === 'updated') { |
28 | $this->updated($this->company); |
29 | } elseif ($this->action === 'deleted') { |
30 | $this->deleted($this->company); |
31 | } |
32 | } |
33 | |
34 | public function backoff() |
35 | { |
36 | return [10, 30, 60, 120, 300]; |
37 | } |
38 | |
39 | private function deleted(Company $company): void |
40 | { |
41 | $this->updateUserInfo([], $company, true); |
42 | } |
43 | |
44 | private function updated(Company $company): void |
45 | { |
46 | $updated = $company->getDirty(); |
47 | |
48 | if (!empty($updated)) { |
49 | $fields = $this->validateUpdatedFields($updated); |
50 | |
51 | if (!empty($fields)) { |
52 | $parsedFields = $this->parseFields($updated); |
53 | |
54 | $this->updateUserInfo($parsedFields, $company); |
55 | } |
56 | } |
57 | } |
58 | |
59 | private function updateUserInfo(array $data, Company $company, ?bool $delete = false) |
60 | { |
61 | if ($delete) { |
62 | UserInfo::where('company_id', $company->id)->update(['company_name' => '', 'company' => '']); |
63 | } elseif ($data['company_name']) { |
64 | UserInfo::where('company_id', $company->id)->update(['company_name' => $data['company_name'], 'company' => $data['company_name']]); |
65 | } |
66 | } |
67 | |
68 | private function validateUpdatedFields(array $updated): array |
69 | { |
70 | $hubspotFields = [ |
71 | 'name', |
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 | 'name' => ['rename' => 'company_name'], |
81 | ]; |
82 | |
83 | return $this->mapObject($fields, $map); |
84 | } |
85 | } |