Completed
Push — master ( 356572...7d3154 )
by Sergey
03:26
created

PaginationHelper::responseHasData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers\Providers;
4
5
trait PaginationHelper
6
{
7
    /**
8
     * Iterate through results of Api function call. By
9
     * default generator will return all pagination results.
10
     * To limit result batches, set $batchesLimit. Call function
11
     * of object to get data.
12
     *
13
     * @param callable $callback
14
     * @param array    $params
15
     * @param int      $batchesLimit
16
     * @return \Iterator
17
     */
18
    public function getPaginatedData($callback, $params, $batchesLimit = 0)
19
    {
20
        $batchesNum = 0;
21
        do {
22
            $response = self::getPaginatedResponse($callback, $params);
23
            $items = self::getDataFromPaginatedResponse($response);
24
            if (empty($items)) return;
25
26
            $batchesNum++;
27
            yield $items;
28
29
            $params['bookmarks'] = self::getBookMarks($response);
30
            if ($this->checkEndBookMarks($params['bookmarks'])) return;
31
32
        } while ( ! self::reachBatchesLimit($batchesLimit, $batchesNum));
33
    }
34
35
    protected function getPaginatedResponse(callable $callback, array $params)
36
    {
37
        $response = call_user_func_array($callback, $params);
38
        if (self::responseHasData($response)) {
39
            return self::clearResponseFromMetaData($response);
40
        }
41
42
        return [];
43
    }
44
45
    protected function getDataFromPaginatedResponse($response)
46
    {
47
        if (self::responseHasData($response)) {
48
            $res = self::clearResponseFromMetaData($response);
49
50
            return $res['data'];
51
        }
52
53
        return [];
54
    }
55
56
    /**
57
     * @param array $res
58
     * @return bool
59
     */
60
    protected function responseHasData($res)
61
    {
62
        return isset($res['data']) && ! empty($res['data']);
63
    }
64
65
    /**
66
     * Check if we get batches limit in pagination
67
     * @param int $batchesLimit
68
     * @param int $batchesNum
69
     * @return bool
70
     */
71
    protected function reachBatchesLimit($batchesLimit, $batchesNum)
72
    {
73
        return $batchesLimit && $batchesNum >= $batchesLimit;
74
    }
75
76
    /**
77
     * Remove 'module' data from response
78
     * @param array $res
79
     * @return array mixed
80
     */
81
    protected function clearResponseFromMetaData($res)
82
    {
83
        if (isset($res['data'][0]['type']) && $res['data'][0]['type'] == 'module') {
84
            array_shift($res['data']);
85
        }
86
87
        return $res;
88
    }
89
90
    /**
91
     * @param $response
92
     * @return array
93
     */
94
    protected function getBookMarks($response)
95
    {
96
        return isset($response['bookmarks']) ? $response['bookmarks'] : [];
97
    }
98
99
    protected function checkEndBookMarks($bookmarks)
100
    {
101
        return ! empty($bookmarks) && $bookmarks[0] == '-end-';
102
    }
103
}
104