Passed
Push — master ( 608424...4542f8 )
by Adam
05:16 queued 03:24
created

TableRequest::deleteRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.9332
cc 1
nc 1
nop 2
crap 1
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 9
    public static function createRecords(string $tableName, array $records): Request
13
    {
14 9
        Assert::that($tableName)
15 9
            ->notEmpty('Table name must not be empty');
16
17 8
        Assert::that($records)
18 8
            ->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 7
        $records = array_values($records);
23
24 7
        foreach ($records as $idx => &$record) {
25 7
            Assert::that($record)
26 7
                ->keyExists('fields', sprintf('Record[%d] should contain a "fields" entry', $idx));
27
28 6
            Assert::that($record['fields'])
29 6
                ->isArray(sprintf('Record[%d] "fields" should be Array', $idx));
30
31 5
            if (empty($record['fields'])) {
32 2
                $record['fields'] = new \stdClass(); // API requires fields to be object
33
            } else {
34 3
                Assert::thatAll(array_keys($record['fields']))
35 3
                    ->string('All "fields" must be string-keyed');
36
            }
37
        }
38
39 4
        return new self(
40 4
            'POST',
41
            $tableName,
42
            [
43 4
                'Content-Type' => 'application/json',
44
            ],
45 4
            json_encode(
46
                [
47 4
                    'records' => $records
48
                ]
49
            )
50
        );
51
    }
52
53 4
    public static function readRecords(string $tableName, string $recordId): Request
54
    {
55 4
        Assert::that($tableName)
56 4
            ->notEmpty('Table name must not be empty');
57 3
        Assert::that($recordId)
58 3
            ->notEmpty('Record Id must not be empty');
59
60 2
        return new self(
61 2
            'GET',
62 2
            $tableName . '/' . $recordId
63
        );
64
    }
65
66 13
    public static function updateRecords(string $tableName, array $records, string $type): Request
67
    {
68 13
        Assert::that($tableName)
69 13
            ->notEmpty('Table name must not be empty');
70
71 12
        Assert::that($records)
72 12
            ->notEmpty('Records must not be empty');
73
74 11
        Assert::that(strtoupper($type))
75 11
            ->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 9
        $records = array_values($records);
80
81 9
        foreach ($records as $idx => $record) {
82 9
            Assert::that($record)
83 9
                ->keyExists('id', sprintf('Record[%d] requires an "id" entry', $idx))
84 8
                ->notEmptyKey('id', sprintf('Record[%d] requires "id" to not be empty', $idx))
85 7
                ->keyExists('fields', sprintf('Record[%d] should contain a "fields" entry', $idx));
86
87 6
            Assert::that($record['fields'])
88 6
                ->isArray(sprintf('Record[%d] "fields" should be Array', $idx))
89 5
                ->notEmpty(sprintf('Record[%d] "fields" should not be empty', $idx));
90
91 4
            Assert::thatAll(array_keys($record['fields']))
92 4
                ->string(sprintf('Record[%d] "fields" should be string-keyed', $idx));
93
        }
94
95 3
        return new self(
96 3
            strtoupper($type),
97
            $tableName,
98
            [
99 3
                'Content-Type' => 'application/json',
100
            ],
101 3
            json_encode(
102
                [
103 3
                    'records' => $records
104
                ]
105
            )
106
        );
107
    }
108
109 2
    public static function deleteRecords(string $tableName, string $recordId): Request
110
    {
111 2
        Assert::that($tableName)
112 2
            ->notEmpty('Table name must not be empty');
113
114 2
        Assert::that($recordId)
115 2
            ->notEmpty('Record Id must not be empty');
116
117
        $deleteRecords = [
118
            'records' => [
119 2
                $recordId
120
            ]
121
        ];
122
123 2
        return new self(
124 2
            'DELETE',
125 2
            $tableName . '?' . http_build_query($deleteRecords)
126
        );
127
    }
128
129 1
    public static function listRecords(string $tableName, array $params)
130
    {
131 1
        Assert::that($tableName)
132
            ->notEmpty('Table name must not be empty');
133 1
134 1
        $queryString = http_build_query($params);
135 1
136
        return new self(
137
            'GET',
138
            $tableName . '?' . $queryString
139
        );
140
141
    }
142
}
143