Passed
Push — master ( 8fd0b9...c0bcb1 )
by Adam
54s queued 12s
created

Table::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 1
c 2
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
     * Table constructor.
35
     *
36
     * @param string $tableName
37
     */
38 9
    public function __construct(string $tableName)
39
    {
40 9
        $this->tableName = $tableName;
41 9
    }
42
43
    /**
44
     * @param Client $client
45
     */
46 9
    public function setClient(Client $client): void
47
    {
48 9
        $this->client = $client;
49 9
    }
50
51
    /**
52
     * @return string|null
53
     */
54 3
    public function getName(): ?string
55
    {
56 3
        return $this->tableName;
57
    }
58
59
    /**
60
     *
61
     * Params could include the following:
62
     *      fields = ['column_1_name', 'column_2_name', 'column_3_name]
63
     *      filterByFormula = "NOT({Headline} = '')"
64
     *      maxRecords = 100
65
     *      pageSize = 100
66
     *      sort = [{field: "Headline", direction: "desc"}]
67
     *      view = "view_name"
68
     *
69
     * @param array $params
70
     * @return ResponseInterface
71
     */
72 1
    public function list(array $params): ResponseInterface
73
    {
74
//        if (!empty($params))
75 1
        $queryString = http_build_query($params);
76
77 1
        $url = $this->tableName . '?' . $queryString;
78
79 1
        return $this->client->request('GET', $url);
80
    }
81
82
    /**
83
     * @param array $records
84
     * @return mixed
85
     */
86 1
    public function create(array $records)
87
    {
88 1
        return $this->client->send(
89 1
            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

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