Passed
Push — master ( bb45ef...eeb9eb )
by Adam
08:12 queued 40s
created

TableRequest::updateRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.8333
cc 1
nc 1
nop 3
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 1
    public static function createRecords(string $tableName, array $records): Request
13
    {
14 1
        Assert::thatAll($records)
15 1
            ->keyExists('fields');
16
17 1
        return new self(
18 1
            'POST',
19
            $tableName,
20
            [
21 1
                'Content-Type' => 'application/json',
22
            ],
23 1
            json_encode(
24
                [
25 1
                    'records' => $records
26
                ]
27
            )
28
        );
29
    }
30
31 1
    public static function readRecords(string $tableName, string $id): Request
32
    {
33 1
        Assert::that($tableName)
34 1
            ->notEmpty();
35 1
        Assert::that($id)
36 1
            ->notEmpty();
37
38 1
        return new self(
39 1
            'GET',
40 1
            $tableName . '/' . $id
41
        );
42
    }
43
44 3
    public static function updateRecords(string $tableName, array $records, string $type): Request
45
    {
46 3
        Assert::that($tableName)
47 3
            ->notEmpty();
48 3
        Assert::thatAll($records)
49 3
            ->keyExists('fields')
50
            ->keyExists('id');
51 2
        Assert::that(strtoupper($type))
52 2
            ->inArray(['PUT', 'PATCH']);
53
54
        return new self(
55 2
            strtoupper($type),
56
            $tableName,
57 2
            [
58
                'Content-Type' => 'application/json',
59 2
            ],
60
            json_encode(
61
                [
62
                    'records' => $records
63
                ]
64
            )
65
        );
66
    }
67
}
68