Query::request()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.9
1
<?php
2
namespace Javis\JsonApi;
3
4
class Query
5
{
6
    // @var Client
7
    protected $api_client;
8
    protected $endpoint;
9
    protected $method;
10
11
    protected $includes = [];
12
    protected $fields = [];
13
    protected $filters = [];
14
    protected $multipart = false;
15
    protected $query = [];
16
    protected $limit;
17
    protected $offset;
18
    protected $formData;
19
    protected $jsonData;
20
    protected $json = [];
21
    protected $throwException = true;
22
23
    public function __construct(Client $api_client, $endpoint)
24
    {
25
        $this->api_client = $api_client;
26
        $this->endpoint = $endpoint;
27
    }
28
29
    /**
30
     * @param array $includes
31
     * @return $this
32
     */
33
    public function includes(array $includes)
34
    {
35
        $this->includes = $includes;
36
        return $this;
37
    }
38
39
    /**
40
     * @param array $fields
41
     * @return $this
42
     */
43
    public function fields(array $fields)
44
    {
45
        $this->fields = $fields;
46
        return $this;
47
    }
48
49
    /**
50
     * @param array $filters
51
     * @return $this
52
     */
53
    public function filter(array $filters)
54
    {
55
        $this->filters = $filters;
56
        return $this;
57
    }
58
59
    /**
60
     * @param array $query
61
     * @return $this
62
     */
63
    public function withQuery(array $query)
64
    {
65
        $this->query = $query;
66
        return $this;
67
    }
68
69
    /**
70
     * @param $data
71
     * @return $this
72
     */
73
    public function withFormData($data)
74
    {
75
        $this->formData = $data;
76
77
        foreach ($data as $d) {
78
            if ($d instanceof \SplFileInfo) {
79
                $this->multipart = true;
80
            }
81
        }
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param $data
88
     * @return $this
89
     */
90
    public function withJsonData($data)
91
    {
92
        $this->jsonData = $data;
93
        return $this;
94
    }
95
96
    /**
97
     * @param $limit
98
     * @param int $offset
99
     * @return $this
100
     */
101
    public function limit($limit, $offset = 0)
102
    {
103
        $this->limit = $limit;
104
        $this->offset = $offset;
105
        return $this;
106
    }
107
108
109
110
    /**
111
     * Build query params array
112
     * @return array
113
     */
114
    protected function buildQuery()
115
    {
116
        $query = [];
117
        if (!empty($this->query)) {
118
            $query = $this->query;
119
        }
120
        if ($this->limit || $this->offset) {
121
            $query['page'] = [];
122
            if ($this->limit) {
123
                $query['page']['limit'] = $this->limit;
124
            }
125
            if ($this->offset) {
126
                $query['page']['offset'] = $this->offset;
127
            }
128
        }
129
130
        if (!empty($this->filters)) {
131
            foreach ($this->filters as $resource => $columns) {
132
                if (is_array($columns)) {
133
                    foreach ($columns as $column => $operands) {
134
                        foreach ($operands as $operand => $value) {
135
                            $query['filter'][$resource][$column][$operand] = is_array($value) ? implode(',',
136
                                $value) : $value;
137
                        }
138
                    }
139
                } else{
140
                    $query['filter'][$resource] = $columns;
141
                }
142
            }
143
        }
144
        if (!empty($this->fields)) {
145
            foreach ($this->fields as $resource => $fieldList) {
146
                $query['fields'][$resource] = implode(',', $fieldList);
147
            }
148
        }
149
        if (!empty($this->includes)) {
150
            $query['include'] = implode(',', $this->includes);
151
        }
152
        return $query;
153
    }
154
155
    /**
156
     * @return array
157
     */
158
    protected function getHeaders()
159
    {
160
        $headers = [];
161
        return $headers;
162
    }
163
164
    protected function request($type)
165
    {
166
        $params = [];
167
        $params['headers'] = $this->getHeaders();
168
        $params['query'] = $this->buildQuery();
169
170
        if (!empty($this->jsonData)) {
171
            $params['json'] = $this->jsonData;
172
        }
173
        else{
174
            if ($this->multipart) {
175
                $params['multipart'] = $this->convertFormDataIntoMultipart($this->formData);
176
            } else {
177
                $params['form_params'] = $this->formData;
178
            }
179
        }
180
181
        return $this->api_client->request($type, $this->endpoint, $params);
182
    }
183
184
    /**
185
     * @param array $data
186
     * @return array
187
     */
188
    protected function convertFormDataIntoMultipart($data = [])
189
    {
190
        $res = [];
191
        foreach ($data as $name => $value) {
192
            $row = ['name' => $name];
193
194
            if ($value instanceof \SplFileInfo) {
195
                $row['contents'] = fopen($value->getPath(), 'r');
196
                $row['filename'] = $value->getFilename();
197
            } else {
198
                $row['contents'] = $value;
199
            }
200
201
            $res[] = $row;
202
        }
203
        return $res;
204
    }
205
206
    /**
207
     * Do a GET request to API
208
     * @return Response
209
     */
210
    public function get()
211
    {
212
        return $this->request('GET');
213
    }
214
215
    /**
216
     * Do a POST request to API
217
     * @return Response
218
     */
219
    public function post()
220
    {
221
        return $this->request('POST');
222
    }
223
224
    /**
225
     * Do a PATCH request to API
226
     * @return Response
227
     */
228
    public function patch()
229
    {
230
        return $this->request('PATCH');
231
    }
232
233
    /**
234
     * Do a DELETE request to API
235
     * @return Response
236
     */
237
    public function delete()
238
    {
239
        return $this->request('DELETE');
240
    }
241
}
242