Passed
Push — master ( f03bc5...7bdbd5 )
by Adam
42s queued 11s
created

Table   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 57.58%

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 9
eloc 30
c 10
b 0
f 1
dl 0
loc 130
ccs 19
cts 33
cp 0.5758
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A read() 0 3 1
A list() 0 13 1
A __construct() 0 4 1
A setClient() 0 3 1
A getName() 0 3 1
A delete() 0 7 1
A update() 0 14 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 3
    public function __construct(string $tableName, string $viewName = "Grid view")
45
    {
46 3
        $this->tableName = $tableName;
47 3
        $this->viewName = $viewName;
48 3
    }
49
50
    /**
51
     * @param Client $client
52
     */
53 3
    public function setClient(Client $client): void
54
    {
55 3
        $this->client = $client;
56 3
    }
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
    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

79
    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...
80
    {
81
        $params = [
82
            'maxRecords' => 3,
83
            'view' => $this->viewName
84
        ];
85
86
//        if (!empty($params))
87
        $queryString = http_build_query($params);
88
89
        $url = $this->tableName . '?' . $queryString;
90
91
        return $this->client->request('GET', $url);
92
    }
93
94
    /**
95
     * @param array $records
96
     * @return mixed
97
     */
98 1
    public function create(array $records)
99
    {
100 1
        return $this->client->send(
101 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

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