Completed
Push — master ( af83f4...b4262b )
by Sergey
03:54 queued 01:20
created

Pagination::callProviderRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
use seregazhuk\PinterestBot\Api\Providers\Provider;
6
7
class Pagination
8
{
9
    /**
10
     * @var Provider
11
     */
12
    private $provider;
13
14
    /**
15
     * @var array
16
     */
17
    protected $bookmarks = [];
18
19
    public function __construct(Provider $provider)
20
    {
21
        $this->provider = $provider;
22
    }
23
24
    /**
25
     * Iterate through results of Api function call. By
26
     * default generator will return all pagination results.
27
     * To limit result batches, set $batchesLimit. Call function
28
     * of object to get data.
29
     *
30
     * @param string $method
31
     * @param array $params
32
     * @param int $batchesLimit
33
     * @return \Iterator
34
     */
35
    public function paginate($method, $params, $batchesLimit = 0)
36
    {
37
        $batchesNum = 0;
38
        do {
39
            $results = $this->callProviderRequest($method, $params);
40
            if (empty($results) || $this->checkEndBookMarks()) {
41
                return;
42
            }
43
44
            $batchesNum++;
45
            foreach ($results as $result) {
46
                yield $result;
47
            }
48
49
        } while (!$this->reachBatchesLimit($batchesLimit, $batchesNum));
50
    }
51
52
    protected function callProviderRequest($method, array $params)
53
    {
54
        $params['bookmarks'] = $this->bookmarks;
55
        $response = call_user_func_array([$this->provider, $method], $params);
56
57
        if ($this->responseHasData($response)) {
58
            $this->getBookMarks($response);
59
60
            return $this->getDataFromPaginatedResponse($response);
61
        }
62
63
        return [];
64
    }
65
66
    protected function getDataFromPaginatedResponse($response)
67
    {
68
        if ($this->responseHasData($response)) {
69
            $res = $this->clearResponseFromMetaData($response);
70
71
            return $res['data'];
72
        }
73
74
        return [];
75
    }
76
77
    /**
78
     * @param array $response
79
     *
80
     * @return bool
81
     */
82
    protected function responseHasData($response)
83
    {
84
        return isset($response['data']) && !empty($response['data']);
85
    }
86
87
    /**
88
     * Check if we get batches limit in pagination.
89
     *
90
     * @param int $batchesLimit
91
     * @param int $batchesNum
92
     *
93
     * @return bool
94
     */
95
    protected function reachBatchesLimit($batchesLimit, $batchesNum)
96
    {
97
        return $batchesLimit && $batchesNum >= $batchesLimit;
98
    }
99
100
    /**
101
     * Remove 'module' data from response.
102
     *
103
     * @param array $response
104
     *
105
     * @return array mixed
106
     */
107
    protected function clearResponseFromMetaData($response)
108
    {
109
        if (isset($response['data'][0]['type']) && $response['data'][0]['type'] == 'module') {
110
            array_shift($response['data']);
111
        }
112
113
        return $response;
114
    }
115
116
    /**
117
     * @param $response
118
     *
119
     * @return array
120
     */
121
    protected function getBookMarks($response)
122
    {
123
        $this->bookmarks = isset($response['bookmarks']) ? $response['bookmarks'] : [];
124
125
        return $this;
126
    }
127
128
    protected function checkEndBookMarks()
129
    {
130
        return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-';
131
    }
132
}
133