Completed
Pull Request — master (#112)
by Sergey
02:26
created

Pagination   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 21
c 8
b 3
f 0
lcom 1
cbo 0
dl 0
loc 129
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B run() 0 19 5
A callProviderRequest() 0 13 2
A getDataFromPaginatedResponse() 0 10 2
A responseHasData() 0 4 2
A reachBatchesLimit() 0 4 2
A clearResponseFromMetaData() 0 8 3
A getBookMarks() 0 6 2
A checkEndBookMarks() 0 4 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 run($method, $params, $batchesLimit = 0)
36
    {
37
        $batchesNum = 0;
38
        do {
39
            $results = $this->callProviderRequest($method, $params);
40
            if (empty($results)) {
41
                return;
42
            }
43
44
            $batchesNum++;
45
            foreach ($results as $result) {
46
                yield $result;
47
            }
48
49
            if ($this->checkEndBookMarks())
50
                return;
51
            
52
        } while (!$this->reachBatchesLimit($batchesLimit, $batchesNum));
53
    }
54
55
    protected function callProviderRequest($method, array $params)
56
    {
57
        $params['bookmarks'] = $this->bookmarks;
58
        $response = call_user_func_array([$this->provider, $method], $params);
59
60
        if ($this->responseHasData($response)) {
61
            $this->getBookMarks($response);
62
63
            return $this->getDataFromPaginatedResponse($response);
64
        }
65
66
        return [];
67
    }
68
69
    protected function getDataFromPaginatedResponse($response)
70
    {
71
        if ($this->responseHasData($response)) {
72
            $res = $this->clearResponseFromMetaData($response);
73
74
            return $res['data'];
75
        }
76
77
        return [];
78
    }
79
80
    /**
81
     * @param array $response
82
     *
83
     * @return bool
84
     */
85
    protected function responseHasData($response)
86
    {
87
        return isset($response['data']) && !empty($response['data']);
88
    }
89
90
    /**
91
     * Check if we get batches limit in pagination.
92
     *
93
     * @param int $batchesLimit
94
     * @param int $batchesNum
95
     *
96
     * @return bool
97
     */
98
    protected function reachBatchesLimit($batchesLimit, $batchesNum)
99
    {
100
        return $batchesLimit && $batchesNum >= $batchesLimit;
101
    }
102
103
    /**
104
     * Remove 'module' data from response.
105
     *
106
     * @param array $response
107
     *
108
     * @return array mixed
109
     */
110
    protected function clearResponseFromMetaData($response)
111
    {
112
        if (isset($response['data'][0]['type']) && $response['data'][0]['type'] == 'module') {
113
            array_shift($response['data']);
114
        }
115
116
        return $response;
117
    }
118
119
    /**
120
     * @param $response
121
     *
122
     * @return array
123
     */
124
    protected function getBookMarks($response)
125
    {
126
        $this->bookmarks = isset($response['bookmarks']) ? $response['bookmarks'] : [];
127
128
        return $this;
129
    }
130
131
    protected function checkEndBookMarks()
132
    {
133
        return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-';
134
    }
135
}
136