1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
3 | |||
4 | namespace BristolSU\AirTable\Progress; |
||
5 | |||
6 | use BristolSU\AirTable\Jobs\CreateRecords; |
||
7 | use BristolSU\AirTable\Jobs\FlushRows; |
||
8 | use BristolSU\Support\ActivityInstance\Contracts\ActivityInstanceRepository; |
||
9 | use BristolSU\Support\ModuleInstance\Contracts\ModuleInstanceRepository; |
||
10 | use BristolSU\Support\ModuleInstance\ModuleInstance; |
||
11 | use BristolSU\Support\Progress\Handlers\Handler; |
||
12 | use BristolSU\Support\Progress\ModuleInstanceProgress; |
||
13 | use BristolSU\Support\Progress\Progress; |
||
14 | use Carbon\Carbon; |
||
15 | use Illuminate\Support\Facades\Log; |
||
16 | |||
17 | class AirtableHandler implements Handler |
||
0 ignored issues
–
show
|
|||
18 | { |
||
19 | |||
20 | protected string $baseId; |
||
21 | protected string $tableName; |
||
22 | protected string $apiKey; |
||
23 | private bool $debug; |
||
0 ignored issues
–
show
|
|||
24 | |||
25 | 3 | public function __construct(string $baseId, string $tableName, string $apiKey, bool $debug = false) |
|
0 ignored issues
–
show
|
|||
26 | { |
||
27 | 3 | $this->baseId = $baseId; |
|
28 | 3 | $this->tableName = $tableName; |
|
29 | 3 | $this->apiKey = $apiKey; |
|
30 | 3 | $this->debug = $debug; |
|
31 | 3 | } |
|
32 | |||
33 | 3 | protected function filterModules(\Closure $filter, Progress $progress, $moduleInstances) |
|
0 ignored issues
–
show
|
|||
34 | { |
||
35 | 3 | return collect($progress->getModules()) |
|
36 | ->filter($filter)->map(function (ModuleInstanceProgress $moduleInstanceProgress) use ($moduleInstances) { |
||
0 ignored issues
–
show
|
|||
37 | 2 | return $moduleInstances[$moduleInstanceProgress->getModuleInstanceId()]; |
|
38 | 3 | })->values()->toArray(); |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
39 | } |
||
40 | |||
41 | 3 | protected function parseProgress(Progress $progress) |
|
0 ignored issues
–
show
|
|||
42 | { |
||
43 | 3 | $activityInstance = app(ActivityInstanceRepository::class) |
|
44 | 3 | ->getById($progress->getActivityInstanceId()); |
|
45 | 3 | $moduleInstances = app(ModuleInstanceRepository::class) |
|
46 | 3 | ->allThroughActivity($activityInstance->activity) |
|
47 | ->reduce(function ($carry, ModuleInstance $moduleInstance) { |
||
0 ignored issues
–
show
|
|||
48 | 2 | $carry[$moduleInstance->id()] = $moduleInstance->name; |
|
49 | 2 | return $carry; |
|
50 | 3 | }); |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
51 | |||
52 | return [ |
||
53 | 3 | 'Participant Name' => $activityInstance->participantName(), |
|
54 | 'Mandatory Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
55 | 2 | return $moduleInstanceProgress->isMandatory(); |
|
56 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
57 | 'Optional Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
58 | 2 | return !$moduleInstanceProgress->isMandatory(); |
|
59 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
60 | 'Complete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
61 | 2 | return $moduleInstanceProgress->isComplete(); |
|
62 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
63 | 'Incomplete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
64 | 2 | return !$moduleInstanceProgress->isComplete(); |
|
65 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
66 | 'Active Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
67 | 2 | return $moduleInstanceProgress->isActive(); |
|
68 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
69 | 'Inactive Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
70 | 2 | return !$moduleInstanceProgress->isActive(); |
|
71 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
72 | 'Hidden Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
73 | 2 | return !$moduleInstanceProgress->isVisible(); |
|
74 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
75 | 'Visible Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
||
0 ignored issues
–
show
|
|||
76 | 2 | return $moduleInstanceProgress->isVisible(); |
|
77 | 3 | }, $progress, $moduleInstances), |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
78 | 3 | '% Complete' => $progress->getPercentage(), |
|
79 | 3 | 'Activity Instance ID' => $activityInstance->id, |
|
80 | 3 | 'Activity ID' => $progress->getActivityId(), |
|
81 | 3 | 'Participant ID' => $activityInstance->resource_id, |
|
82 | 3 | 'Snapshot Date' => $progress->getTimestamp()->format(\DateTime::ATOM) |
|
83 | ]; |
||
84 | |||
85 | } |
||
86 | |||
87 | /** |
||
0 ignored issues
–
show
|
|||
88 | * @param array|Progress[] $progresses |
||
0 ignored issues
–
show
|
|||
89 | */ |
||
0 ignored issues
–
show
|
|||
90 | 3 | public function saveMany(array $progresses): void |
|
91 | { |
||
92 | 3 | $data = []; |
|
93 | 3 | $this->log(sprintf('Parsing %d progresses', count($progresses))); |
|
94 | 3 | foreach ($progresses as $progress) { |
|
95 | 3 | $data[] = $this->parseProgress($progress); |
|
96 | } |
||
97 | 3 | $this->createRecords($data); |
|
98 | 3 | } |
|
99 | |||
100 | 2 | public function save(Progress $progress): void |
|
0 ignored issues
–
show
|
|||
101 | { |
||
102 | 2 | $this->saveMany([$progress]); |
|
103 | 2 | } |
|
104 | |||
105 | 3 | protected function createRecords(array $data) |
|
0 ignored issues
–
show
|
|||
106 | { |
||
107 | 3 | $this->log('Flushing rows'); |
|
108 | 3 | dispatch_now( |
|
109 | 3 | (new FlushRows($this->apiKey, $this->baseId, $this->tableName)) |
|
110 | 3 | ->withDebug($this->debug) |
|
111 | ); |
||
112 | |||
113 | 3 | $this->log('Flushed rows'); |
|
114 | |||
115 | 3 | foreach(array_chunk($data, 10) as $rows) { |
|
0 ignored issues
–
show
|
|||
116 | 3 | dispatch((new CreateRecords( |
|
0 ignored issues
–
show
|
|||
117 | 3 | $rows, |
|
118 | 3 | $this->apiKey, |
|
119 | 3 | $this->baseId, |
|
120 | 3 | $this->tableName |
|
121 | 3 | ))->withDebug($this->debug)); |
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
122 | } |
||
123 | |||
124 | 3 | $this->log('Created Records'); |
|
125 | 3 | } |
|
126 | |||
127 | 3 | private function log(string $string) |
|
0 ignored issues
–
show
|
|||
128 | { |
||
129 | 3 | if($this->debug) { |
|
0 ignored issues
–
show
|
|||
130 | Log::debug($string); |
||
131 | } |
||
132 | 3 | } |
|
133 | } |
||
134 |