Completed
Push — master ( 89c8a9...bb4dcf )
by Sergey
03:12
created

RequestHelper::parsePaginatedResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4286
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Providers;
4
5
class RequestHelper
6
{
7
    /**
8
     * @param array|object $data
9
     * @param string|null  $sourceUrl
10
     * @param array        $bookmarks
11
     * @return array
12
     */
13
    public static function createRequestData($data = [], $sourceUrl = '/', $bookmarks = [])
14
    {
15
        if (empty($data)) {
16
            $data = self::createEmptyRequestData();
17
        }
18
19
        if ( ! empty($bookmarks)) {
20
            $data["options"]["bookmarks"] = $bookmarks;
21
        }
22
23
        $data["context"] = new \stdClass();
24
25
        return [
26
            "source_url" => $sourceUrl,
27
            "data"       => json_encode($data),
28
        ];
29
    }
30
31
    /**
32
     * Check if specified data exists in response
33
     * @param      $response
34
     * @param null $key
35
     * @return array|bool
36
     */
37
    public static function getDataFromResponse($response, $key = null)
38
    {
39
        if (isset($response['resource_response']['data'])) {
40
            $data = $response['resource_response']['data'];
41
42
            if ($key) {
43
                return array_key_exists($key, $data) ? $data[$key] : false;
44
            }
45
46
            return $data;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Parse bookmarks from response
54
     * @param array $response
55
     * @return string|null
56
     */
57
    protected static function _getBookmarksFromResponse($response)
58
    {
59
        if (isset($response['resource']['options']['bookmarks'][0])) {
60
            return [$response['resource']['options']['bookmarks'][0]];
61
        }
62
63
        return null;
64
    }
65
66
    /**
67
     * Checks result of PIN-methods
68
     *
69
     * @param array $res
70
     * @return bool
71
     */
72
    public static function checkMethodCallResult($res)
73
    {
74
        if ($res !== null && isset($res['resource_response'])) {
75
            return true;
76
        }
77
78
        return false;
79
    }
80
81
    /**
82
     * Checks Pinterest API paginated response, and parses data
83
     * with bookmarks info from it.
84
     *
85
     * @param array $res
86
     * @return array
87
     */
88
    public static function parsePaginatedResponse($res)
89
    {
90
        if ( ! self::checkMethodCallResult($res)) {
91
            return [];
92
        }
93
94
        $bookmarks = self::_getBookmarksFromResponse($res);
95
        if ($data = self::getDataFromResponse($res)) {
96
            return ['data' => $data, 'bookmarks' => $bookmarks];
97
        }
98
99
        return [];
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    protected static function createEmptyRequestData()
106
    {
107
        return array('options' => []);
108
    }
109
110
}