Completed
Push — master ( 31d905...b5d7ba )
by Sergey
05:31 queued 02:23
created

Response   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 9
Bugs 4 Features 1
Metric Value
wmc 18
c 9
b 4
f 1
lcom 1
cbo 0
dl 0
loc 132
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseData() 0 14 4
A getData() 0 8 2
A isEmpty() 0 4 1
A hasErrors() 0 12 3
A getBookmarks() 0 8 3
A getPaginationData() 0 13 4
A getLastError() 0 4 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 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
     * Checks Pinterest API paginated response, and parses data
111
     * with bookmarks info from it.
112
     *
113
     * @param array $response
114
     *
115
     * @return array
116
     */
117
    public function getPaginationData($response)
118
    {
119
        if ($this->isEmpty($response) && $this->hasErrors($response)) {
120
            return [];
121
        }
122
123
        $bookmarks = $this->getBookmarks($response);
124
        if ($data = $this->getData($response)) {
125
            return ['data' => $data, 'bookmarks' => $bookmarks];
126
        }
127
128
        return [];
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getLastError()
135
    {
136
        return $this->lastError;
137
    }
138
}
139