Passed
Branch master (0d5bec)
by Sergey
07:01 queued 03:43
created

PaginationHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 94
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPaginatedData() 0 16 3
A getBookMarks() 0 4 2
A getPaginatedResponse() 0 9 2
A getDataFromPaginatedResponse() 0 10 2
A responseHasData() 0 4 2
A reachBatchesLimit() 0 4 2
A clearResponseFromMetaData() 0 8 3
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
25
            if (empty($items)) {
26
                return;
27
            }
28
29
            $params['bookmarks'] = self::getBookMarks($response);
30
            $batchesNum++;
31
            yield $items;
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