Completed
Pull Request — master (#203)
by Sergey
02:31
created

Pagination::clearResponseFromMetaData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
use seregazhuk\PinterestBot\Api\Providers\Provider;
6
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
7
8
class Pagination
9
{
10
    /**
11
     * @var Provider
12
     */
13
    protected $provider;
14
15
    /**
16
     * @var array
17
     */
18
    protected $bookmarks = [];
19
20
    /**
21
     * @param Provider $provider
22
     */
23
    public function __construct(Provider $provider)
24
    {
25
        $this->provider = $provider;
26
    }
27
28
    /**
29
     * Iterate through results of Api function call. By
30
     * default generator will return all pagination results.
31
     * To limit result batches, set $limit. Call function
32
     * of object to get data.
33
     *
34
     * @param string $method
35
     * @param array $params
36
     * @param int $limit
37
     * @return \Generator|void
38
     */
39
    public function paginateOver($method, $params, $limit = 0)
40
    {
41
        $resultsNum = 0;
42
        while (true) {
43
            $response = $this->callProviderRequest($method, $params);
44
            $results = $this->processProviderResponse($response);
45
46
            if (empty($results)) return;
47
48
            foreach ($results as $result) {
0 ignored issues
show
Bug introduced by
The expression $results of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
                $resultsNum++;
50
                yield $result;
51
52
                if ($this->reachesLimit($limit, $resultsNum) || $this->checkEndBookMarks()) {
53
                    return;
54
                }
55
            }
56
        }
57
58
        return;
59
    }
60
61
    /**
62
     * @param string $method
63
     * @param array $params
64
     * @return PaginatedResponse
65
     */
66
    protected function callProviderRequest($method, array $params)
67
    {
68
        $params['bookmarks'] = $this->bookmarks;
69
70
        return call_user_func_array([$this->provider, $method], $params);
71
    }
72
73
    /**
74
     * @param PaginatedResponse $response
75
     * @return array
76
     */
77
    protected function processProviderResponse(PaginatedResponse $response)
78
    {
79
        if ($response->hasResponseData()) {
80
            $this->bookmarks = $response->getBookmarks();
81
82
            return $response->getResponseData();
83
        }
84
85
        return [];
86
    }
87
88
    /**
89
     * Check if we get batches limit in pagination.
90
     *
91
     * @param int $limit
92
     * @param int $resultsNum
93
     *
94
     * @return bool
95
     */
96
    protected function reachesLimit($limit, $resultsNum)
97
    {
98
        return $limit && $resultsNum >= $limit;
99
    }
100
101
102
    /**
103
     * Checks for -end- substring in bookmarks
104
     *
105
     * @return bool
106
     */
107
    protected function checkEndBookMarks()
108
    {
109
        return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-';
110
    }
111
}
112