Passed
Push — master ( 7bdbd5...8fd0b9 )
by Adam
40s queued 12s
created

Table::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 8
c 3
b 0
f 1
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Project airtable-sdk-php
4
 * File: Table.php
5
 * Created by: tpojka
6
 * On: 26/03/2020
7
 */
8
9
declare(strict_types=1);
10
11
namespace Beachcasts\Airtable;
12
13
use Beachcasts\Airtable\Request\TableRequest as TableRequest;
14
use GuzzleHttp\Client;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Class Table
19
 * @package Beachcasts\Airtable
20
 */
21
class Table
22
{
23
    /**
24
     * @var string|null $tableName
25
     */
26
    protected $tableName;
27
28
    /**
29
     * @var Client $client
30
     */
31
    protected $client;
32
33
    /**
34
     * @var string $viewName
35
     */
36
    protected $viewName;
37
38
    /**
39
     * Table constructor.
40
     *
41
     * @param string $tableName
42
     * @param string $viewName
43
     */
44 8
    public function __construct(string $tableName, string $viewName = "Grid view")
45
    {
46 8
        $this->tableName = $tableName;
47 8
        $this->viewName = $viewName;
48 8
    }
49
50
    /**
51
     * @param Client $client
52
     */
53 8
    public function setClient(Client $client): void
54
    {
55 8
        $this->client = $client;
56 8
    }
57
58
    /**
59
     * @return string|null
60
     */
61 2
    public function getName(): ?string
62
    {
63 2
        return $this->tableName;
64
    }
65
66
    /**
67
     *
68
     * Params could include the following:
69
     *      fields = ['column_1_name', 'column_2_name', 'column_3_name]
70
     *      filterByFormula = "NOT({Headline} = '')"
71
     *      maxRecords = 100
72
     *      pageSize = 100
73
     *      sort = [{field: "Headline", direction: "desc"}]
74
     *      view = "view_name"
75
     *
76
     * @param array $params
77
     * @return ResponseInterface
78
     */
79 1
    public function list(array $params): ResponseInterface
80
    {
81
//        if (!empty($params))
82
        $queryString = http_build_query($params);
83
84
        $url = $this->tableName . '?' . $queryString;
85
86
        return $this->client->request('GET', $url);
87 1
    }
88
89 1
    /**
90
     * @param array $records
91 1
     * @return mixed
92
     */
93
    public function create(array $records)
94
    {
95
        return $this->client->send(
96
            TableRequest::createRecords($this->getName(), $records)
0 ignored issues
show
Bug introduced by
It seems like $this->getName() can also be of type null; however, parameter $tableName of Beachcasts\Airtable\Requ...equest::createRecords() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            TableRequest::createRecords(/** @scrutinizer ignore-type */ $this->getName(), $records)
Loading history...
97
        );
98 1
    }
99
100 1
    /**
101 1
     * @param string $id
102
     * @return mixed
103
     */
104
    public function read(string $id)
105
    {
106
        return $this->client->request('GET', $this->tableName . '/' . $id);
107
    }
108
109 1
    /**
110
     * @todo split out to a replace method for PUT
111 1
     *
112
     * @param array $data
113
     * @param string $type accepts PUT to replace or PATCH to update records
114
     * @return mixed
115
     * @throws \Exception
116
     */
117
    public function update(array $data, $type = 'PATCH')
118
    {
119
        if (!in_array(strtolower($type), ['put', 'patch'])) {
120
            throw new \Exception('Invalid method type.');
121
        }
122 3
123
        return $this->client->request(
124 3
            strtoupper($type),
125 1
            $this->tableName,
126
            [
127
                'headers' => [
128 2
                    'Content-Type' => 'application/json',
129 2
                ],
130 2
                'body' => json_encode($data),
131
            ]
132
        );
133 2
    }
134
135 2
    /**
136
     * @param string $id
137
     * @return mixed
138
     */
139
    public function delete(string $id)
140
    {
141
        return $this->client->request(
142
            'DELETE',
143
            $this->tableName,
144 1
            [
145
                'query' => ['records[]' => $id],
146 1
            ]
147 1
        );
148 1
    }
149
}
150