Completed
Pull Request — master (#237)
by Sergey
02:43
created

Response   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 26
c 3
b 2
f 0
lcom 1
cbo 0
dl 0
loc 208
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A fill() 0 10 1
A fillFromJson() 0 4 1
A getResponseData() 0 8 2
A getData() 0 4 1
A hasData() 0 4 1
A parseResponseData() 0 9 3
A getValueByKey() 0 16 4
A isEmpty() 0 4 1
A isOk() 0 4 1
A hasErrors() 0 4 1
A getBookmarks() 0 10 3
A getRawBookmarksData() 0 4 1
A getPaginationData() 0 12 3
A getLastError() 0 4 1
A getClientInfo() 0 4 1
A getRawData() 0 4 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
6
7
class Response implements PaginatedResponse
8
{
9
    /**
10
     * @var mixed
11
     */
12
    protected $data = [];
13
14
    /**
15
     * @var array|null
16
     */
17
    protected $lastError;
18
19
    /**
20
     * @var array|null
21
     */
22
    protected $clientInfo;
23
24
    /**
25
     * @param mixed $data
26
     * @return $this
27
     */
28
    public function fill($data)
29
    {
30
        $this->data = $data;
31
32
        $this->lastError = $this->getValueByKey('resource_response.error', $this->data);
33
34
        $this->clientInfo = $this->getValueByKey('client_context', $this->data);
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $json
41
     * @return static
42
     */
43
    public function fillFromJson($json)
44
    {
45
        return $this->fill(json_decode($json, true));
46
    }
47
48
    /**
49
     * Check if specified data exists in response.
50
     *
51
     * @param null  $key
52
     * @return array|bool
53
     */
54
    public function getResponseData($key = null)
55
    {
56
        if ($this->hasErrors()) {
57
            return false;
58
        }
59
60
        return $this->parseResponseData($key);
61
    }
62
63
    /**
64
     * @param string $key
65
     * @param null $default
66
     * @return mixed
67
     */
68
    public function getData($key = '', $default = null)
69
    {
70
        return $this->getValueByKey($key, $this->data, $default);
71
    }
72
73
    /**
74
     * @param string $key
75
     * @return bool
76
     */
77
    public function hasData($key = '')
78
    {
79
        return !is_null($this->getValueByKey($key, $this->data));
80
    }
81
82
    /**
83
     * Parse data from Pinterest Api response.
84
     * Data is stored in ['resource_response']['data'] array.
85
     *
86
     * @param string $key
87
     * @return bool|array
88
     */
89
    protected function parseResponseData($key)
90
    {
91
        $responseData = $this->getValueByKey('resource_response.data', $this->data);
92
        if(!$responseData) return false;
93
94
        return $key ?
95
            $this->getValueByKey($key, $responseData) :
96
            $responseData;
97
    }
98
99
    /**
100
     * @param string $key
101
     * @param array $data
102
     * @param mixed $default
103
     * @return array|bool|mixed
104
     */
105
    protected function getValueByKey($key = '', $data, $default = null)
106
    {
107
        if(empty($key)) return $data;
108
109
        $indexes = explode('.', $key);
110
111
        $value = $data;
112
113
        foreach ($indexes as $index) {
114
            if(!isset($value[$index])) return $default;
115
116
            $value = $value[$index];
117
        }
118
119
        return $value;
120
    }
121
122
    /**
123
     * Checks if response is empty.
124
     *
125
     * @return bool
126
     */
127
    public function isEmpty()
128
    {
129
        return empty($this->getResponseData());
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isOk()
136
    {
137
        return !$this->hasErrors();
138
    }
139
140
    /**
141
     * Check for error info in api response and save
142
     * it.
143
     *
144
     * @return bool
145
     */
146
    public function hasErrors()
147
    {
148
        return !empty($this->lastError);
149
    }
150
151
    /**
152
     * Parse bookmarks from response.
153
     *
154
     * @return array
155
     */
156
    public function getBookmarks()
157
    {
158
        $bookmarks = $this->getRawBookmarksData();
159
160
        if (empty($bookmarks)) return [];
161
162
        if ($bookmarks[0] == '-end-') return [];
163
164
        return $bookmarks;
165
    }
166
167
    protected function getRawBookmarksData()
168
    {
169
        return $this->getData('resource.options.bookmarks', []);
170
    }
171
172
    /**
173
     * Checks Pinterest API paginated response, and parses data
174
     * with bookmarks info from it.
175
     *
176
     * @return array
177
     */
178
    public function getPaginationData()
179
    {
180
        if ($this->hasErrors()) return [];
181
182
        $bookmarks = $this->getBookmarks();
183
184
        if ($data = $this->getResponseData()) {
185
            return ['data' => $data, 'bookmarks' => $bookmarks];
186
        }
187
188
        return [];
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    public function getLastError()
195
    {
196
        return $this->lastError;
197
    }
198
199
    /**
200
     * @return array|null
201
     */
202
    public function getClientInfo()
203
    {
204
        return $this->clientInfo;
205
    }
206
207
    /**
208
     * @return mixed
209
     */
210
    public function getRawData()
211
    {
212
        return $this->data;
213
    }
214
}
215