Completed
Pull Request — master (#111)
by Sergey
03:15
created

Response::parseSearch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Api;
4
5
use seregazhuk\PinterestBot\Contracts\ResponseInterface;
6
7
class Response implements ResponseInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $response;
13
14
    /**
15
     * @var array|null
16
     */
17
    protected $lastError;
18
19
    /**
20
     * Check if specified data exists in response.
21
     *
22
     * @param array $response
23
     * @param null  $key
24
     *
25
     * @return array|bool
26
     */
27
    public function getData($response, $key = null)
28
    {
29
        if (!$this->hasErrors($response)) {
30
            return false;
31
        }
32
33
        return $this->parseData($response, $key);
34
    }
35
36
    /**
37
     * Parse data from Pinterest Api response.
38
     * Data stores in ['resource_response']['data'] array.
39
     *
40
     * @param array  $response
41
     * @param string $key
42
     *
43
     * @return bool|array
44
     */
45
    protected function parseData($response, $key)
46
    {
47
        if (isset($response['resource_response']['data'])) {
48
            $data = $response['resource_response']['data'];
49
50
            if ($key) {
51
                return array_key_exists($key, $data) ? $data[$key] : false;
52
            }
53
54
            return $data;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * Checks if response is not empty.
62
     *
63
     * @param array $response
64
     *
65
     * @return bool
66
     */
67
    public function isEmpty($response)
68
    {
69
        return !empty($this->getData($response));
70
    }
71
72
    /**
73
     * Check for error info in api response and save
74
     * it.
75
     *
76
     * @param array $response
77
     *
78
     * @return bool
79
     */
80
    public function hasErrors($response)
81
    {
82
        $this->lastError = null;
83
84
        if (isset($response['resource_response']['error']) && !empty($response['resource_response']['error'])) {
85
            $this->lastError = $response['resource_response']['error'];
86
87
            return false;
88
        }
89
90
        return true;
91
    }
92
93
    /**
94
     * Parse bookmarks from response.
95
     *
96
     * @param array $response
97
     *
98
     * @return array|null
99
     */
100
    public function getBookmarks($response)
101
    {
102
        if ($this->hasErrors($response) && isset($response['resource']['options']['bookmarks'][0])) {
103
            return [$response['resource']['options']['bookmarks'][0]];
104
        }
105
106
        return null;
107
    }
108
109
110
    /**
111
     * Parses Pinterest search API response for data and bookmarks
112
     * for next pagination page.
113
     *
114
     * @param array $response
115
     * @param bool  $bookmarksUsed
116
     *
117
     * @return array|null
118
     */
119
    public function parseSearchWithBookmarks($response, $bookmarksUsed = true)
120
    {
121
        if ($response === null || !$bookmarksUsed) {
122
            return $this->parseSearch($response);
123
        }
124
125
        return $this->getPaginationData($response);
126
    }
127
128
    /**
129
     * Checks Pinterest API paginated response, and parses data
130
     * with bookmarks info from it.
131
     *
132
     * @param array $response
133
     *
134
     * @return array
135
     */
136
    public function getPaginationData($response)
137
    {
138
        if (!$this->isEmpty($response) && !$this->hasErrors($response)) {
139
            return [];
140
        }
141
142
        $bookmarks = $this->getBookmarks($response);
143
        if ($data = $this->getData($response)) {
144
            return ['data' => $data, 'bookmarks' => $bookmarks];
145
        }
146
147
        return [];
148
    }
149
150
    /**
151
     * Parses simple Pinterest search API response
152
     * on request with bookmarks.
153
     *
154
     * @param array $response
155
     *
156
     * @return array
157
     */
158
    public function parseSearch($response)
159
    {
160
        $bookmarks = [];
161
        if (isset($response['module']['tree']['resource']['options']['bookmarks'][0])) {
162
            $bookmarks = $response['module']['tree']['resource']['options']['bookmarks'][0];
163
        }
164
165
        if (!empty($response['module']['tree']['data']['results'])) {
166
            return ['data' => $response['module']['tree']['data']['results'], 'bookmarks' => [$bookmarks]];
167
        }
168
169
        return [];
170
    }
171
172
    /**
173
     * @return array
174
     */
175
    public function getLastError()
176
    {
177
        return $this->lastError;
178
    }
179
}
180