GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 729ed9...157ece )
by Toby
05:11
created

AirtableHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 98.53%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 72
c 3
b 0
f 0
dl 0
loc 114
ccs 67
cts 68
cp 0.9853
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseProgress() 0 42 1
A filterModules() 0 6 1
A __construct() 0 6 1
A log() 0 4 2
A createRecords() 0 20 2
A save() 0 3 1
A saveMany() 0 8 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
Coding Style introduced by
Missing doc comment for class AirtableHandler
Loading history...
18
{
19
20
    protected string $baseId;
21
    protected string $tableName;
22
    protected string $apiKey;
23
    private bool $debug;
0 ignored issues
show
Coding Style introduced by
Private member variable "debug" must be prefixed with an underscore
Loading history...
24
25 3
    public function __construct(string $baseId, string $tableName, string $apiKey, bool $debug = false)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
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
Coding Style introduced by
Missing doc comment for function filterModules()
Loading history...
34
    {
35 3
        return collect($progress->getModules())
36
            ->filter($filter)->map(function (ModuleInstanceProgress $moduleInstanceProgress) use ($moduleInstances) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
37 2
                return $moduleInstances[$moduleInstanceProgress->getModuleInstanceId()];
38 3
            })->values()->toArray();
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
39
    }
40
41 3
    protected function parseProgress(Progress $progress)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function parseProgress()
Loading history...
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
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
48 2
                $carry[$moduleInstance->id()] = $moduleInstance->name;
49 2
                return $carry;
50 3
            });
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
51
52
        return [
53 3
            'Participant Name' => $activityInstance->participantName(),
54
            'Mandatory Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
55 2
                return $moduleInstanceProgress->isMandatory();
56 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
57
            'Optional Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
58 2
                return !$moduleInstanceProgress->isMandatory();
59 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
60
            'Complete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
61 2
                return $moduleInstanceProgress->isComplete();
62 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
63
            'Incomplete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
64 2
                return !$moduleInstanceProgress->isComplete();
65 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
66
            'Active Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
67 2
                return $moduleInstanceProgress->isActive();
68 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
69
            'Inactive Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
70 2
                return !$moduleInstanceProgress->isActive();
71 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
72
            'Hidden Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
73 2
                return !$moduleInstanceProgress->isVisible();
74 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
75
            'Visible Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
76 2
                return $moduleInstanceProgress->isVisible();
77 3
            }, $progress, $moduleInstances),
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
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
Coding Style introduced by
Missing short description in doc comment
Loading history...
88
     * @param array|Progress[] $progresses
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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
Coding Style introduced by
Missing doc comment for function save()
Loading history...
101
    {
102 2
        $this->saveMany([$progress]);
103 2
    }
104
105 3
    protected function createRecords(array $data)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createRecords()
Loading history...
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
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
116 3
            dispatch((new CreateRecords(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
117 3
                $rows,
118 3
                $this->apiKey,
119 3
                $this->baseId,
120 3
                $this->tableName
121 3
            ))->withDebug($this->debug));
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
122
        }
123
        
124 3
        $this->log('Created Records');
125 3
    }
126
127 3
    private function log(string $string)
0 ignored issues
show
Coding Style introduced by
Private method name "AirtableHandler::log" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function log()
Loading history...
128
    {
129 3
        if($this->debug) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
130
            Log::debug($string);
131
        }
132 3
    }
133
}
134