TableRequest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
eloc 70
c 3
b 0
f 1
dl 0
loc 131
ccs 73
cts 73
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A readRecords() 0 10 1
A createRecords() 0 36 3
A updateRecords() 0 38 2
A listRecords() 0 10 1
A deleteRecords() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Beachcasts\Airtable\Request;
6
7
use Assert\Assert;
8
use GuzzleHttp\Psr7\Request;
9
10
class TableRequest extends Request
11
{
12 7
    public static function createRecords(string $tableName, array $records): Request
13
    {
14 7
        Assert::that($tableName)
15 7
            ->notEmpty('Table name must not be empty');
16
17 6
        Assert::that($records)
18 6
            ->notEmpty('Records must not be empty');
19
20
        // we want to make sure we don't pass a k-v array, but numeric indexed
21
        // as api expects records to be of
22 5
        $records = array_values($records);
23
24 5
        foreach ($records as $idx => &$record) {
25 5
            Assert::that($record)
26 5
                ->keyExists('fields', sprintf('Record[%d] should contain a "fields" entry', $idx));
27
28 4
            Assert::that($record['fields'])
29 4
                ->isArray(sprintf('Record[%d] "fields" should be Array', $idx));
30
31 3
            if (empty($record['fields'])) {
32 1
                $record['fields'] = new \stdClass(); // API requires fields to be object
33
            } else {
34 2
                Assert::thatAll(array_keys($record['fields']))
35 2
                    ->string('All "fields" must be string-keyed');
36
            }
37
        }
38
39 2
        return new self(
40 2
            'POST',
41 2
            $tableName,
42
            [
43 2
                'Content-Type' => 'application/json',
44
            ],
45 2
            json_encode(
46
                [
47 2
                    'records' => $records
48
                ]
49
            )
50
        );
51
    }
52
53 3
    public static function readRecords(string $tableName, string $recordId): Request
54
    {
55 3
        Assert::that($tableName)
56 3
            ->notEmpty('Table name must not be empty');
57 2
        Assert::that($recordId)
58 2
            ->notEmpty('Record Id must not be empty');
59
60 1
        return new self(
61 1
            'GET',
62 1
            $tableName . '/' . $recordId
63
        );
64
    }
65
66 10
    public static function updateRecords(string $tableName, array $records, string $type): Request
67
    {
68 10
        Assert::that($tableName)
69 10
            ->notEmpty('Table name must not be empty');
70
71 9
        Assert::that($records)
72 9
            ->notEmpty('Records must not be empty');
73
74 8
        Assert::that(strtoupper($type))
75 8
            ->inArray(['PUT', 'PATCH'], 'Update type should be either PATCH or PUT');
76
77
        // we want to make sure we don't pass a k-v array, but numeric indexed
78
        // as api expects records to be of
79 7
        $records = array_values($records);
80
81 7
        foreach ($records as $idx => $record) {
82 7
            Assert::that($record)
83 7
                ->keyExists('id', sprintf('Record[%d] requires an "id" entry', $idx))
84 6
                ->notEmptyKey('id', sprintf('Record[%d] requires "id" to not be empty', $idx))
85 5
                ->keyExists('fields', sprintf('Record[%d] should contain a "fields" entry', $idx));
86
87 4
            Assert::that($record['fields'])
88 4
                ->isArray(sprintf('Record[%d] "fields" should be Array', $idx))
89 3
                ->notEmpty(sprintf('Record[%d] "fields" should not be empty', $idx));
90
91 2
            Assert::thatAll(array_keys($record['fields']))
92 2
                ->string(sprintf('Record[%d] "fields" should be string-keyed', $idx));
93
        }
94
95 1
        return new self(
96 1
            strtoupper($type),
97 1
            $tableName,
98
            [
99 1
                'Content-Type' => 'application/json',
100
            ],
101 1
            json_encode(
102
                [
103 1
                    'records' => $records
104
                ]
105
            )
106
        );
107
    }
108
109 3
    public static function deleteRecords(string $tableName, array $records): Request
110
    {
111 3
        Assert::that($tableName)
112 3
            ->notEmpty('Table name must not be empty');
113
114 2
        Assert::that($records)
115 2
            ->notEmpty('Records must not be empty');
116
117 1
        $params = [];
118
119 1
        $i = 0;
120 1
        foreach ($records as $record) {
121 1
            $params['records'][] = $record[$i]['id'];
122 1
            $i++;
123
        }
124
125 1
        return new self(
126 1
            'DELETE',
127 1
            $tableName . '?' . http_build_query($params)
128
        );
129
    }
130
131 2
    public static function listRecords(string $tableName, array $params)
132
    {
133 2
        Assert::that($tableName)
134 2
            ->notEmpty('Table name must not be empty');
135
136 1
        $queryString = http_build_query($params);
137
138 1
        return new self(
139 1
            'GET',
140 1
            $tableName . '?' . $queryString
141
        );
142
    }
143
}
144