Passed
Pull Request — master (#20)
by Adam
02:10
created

Table::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
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 GuzzleHttp\Client;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * Class Table
18
 * @package Beachcasts\Airtable
19
 */
20
class Table
21
{
22
    /**
23
     * @var string|null
24
     */
25
    protected $tableName = null;
26
27
    protected $client;
28
29
    protected $viewName;
30
31
    /**
32
     * Table constructor.
33
     *
34
     * @param string $tableName
35
     * @param string $viewName
36
     */
37 2
    public function __construct(string $tableName, string $viewName = "Grid view")
38
    {
39 2
        $this->tableName = $tableName;
40 2
        $this->viewName = $viewName;
41 2
    }
42
43
    /**
44
     * @param Client $client
45
     */
46 2
    public function setClient(Client $client)
47
    {
48 2
        $this->client = $client;
49 2
    }
50
51
    /**
52
     * @return string|null
53
     */
54
    public function getName()
55
    {
56
        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
    public function list(array $params): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

72
    public function list(/** @scrutinizer ignore-unused */ array $params): ResponseInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    {
74
        $params = [
75
            'maxRecords' =>3,
76
            'view' => $this->viewName
77
        ];
78
79
//        if (!empty($params))
80
        $queryString = http_build_query($params);
81
82
        $url = $this->tableName . '?' . $queryString;
83
84
        return $this->client->request(
85
            'GET',
86
            $url,
87
            [
88
                'headers' => [
89
                    'Authorization' => 'Bearer ' . getenv('API_KEY'),
90
                ]
91
            ]
92
        );
93
    }
94
95
    /**
96
     * @param string $data
97
     * @return mixed
98
     */
99 1
    public function create(string $data)
100
    {
101 1
        return $this->client->request(
102 1
            'POST',
103 1
            $this->tableName,
104
            [
105
                'headers' => [
106 1
                    'Authorization' => 'Bearer ' . getenv('API_KEY'),
107 1
                    'Content-Type' => 'application/json',
108
                ],
109 1
                'body' => $data,
110
            ]
111
        );
112
    }
113
114
    /**
115
     * @param string $id
116
     * @return mixed
117
     */
118
    public function read(string $id)
119
    {
120
        return $this->client->request(
121
            'GET',
122
            $this->tableName . '/' . $id,
123
            [
124
                'headers' => [
125
                    'Authorization' => 'Bearer ' . getenv('API_KEY'),
126
                ],
127
            ]
128
        );
129
    }
130
131
    /**
132
     * @param string $data
133
     * @param string $type accepts PUT to replace or PATCH to update records
134
     * @return mixed
135
     * @throws \Exception
136
     */
137
    public function update(string $data, $type = 'PATCH')
138
    {
139
        if (!in_array(strtolower($type), ['put', 'patch'])) {
140
            throw new \Exception('Invalid method type.');
141
        }
142
143
        return $this->client->request(
144
            strtoupper($type),
145
            $this->tableName,
146
            [
147
                'headers' => [
148
                    'Authorization' => 'Bearer ' . getenv('API_KEY'),
149
                    'Content-Type' => 'application/json',
150
                ],
151
                'body' => $data,
152
            ]
153
        );
154
    }
155
156
    /**
157
     * @param string $id
158
     * @return mixed
159
     */
160
    public function delete(string $id)
161
    {
162
        return $this->client->request(
163
            'DELETE',
164
            $this->tableName,
165
            [
166
                'headers' => [
167
                    'Authorization' => 'Bearer ' . getenv('API_KEY'),
168
                ],
169
                'query' => ['records[]' => $id],
170
            ]
171
        );
172
    }
173
}
174