Completed
Pull Request — master (#221)
by Sergey
02:10
created

Pagination::reachesLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 2
eloc 2
nc 2
nop 2
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse;
6
7
class Pagination
8
{
9
    /**
10
     * @var int
11
     */
12
    protected $limit;
13
14
    /**
15
     * @var array
16
     */
17
    protected $bookmarks = [];
18
19
    /**
20
     * @param int $limit
21
     */
22
    public function __construct($limit = 0)
23
    {
24
        $this->limit = $limit;
25
    }
26
27
    /**
28
     * Iterate through results of Api function call. By
29
     * default generator will return all pagination results.
30
     * To limit results, set $limit.
31
     *
32
     * @param callable $callback
33
     * @return \Generator
34
     */
35
    public function paginateOver(callable $callback)
36
    {
37
        $resultsNum = 0;
38
        while (true) {
39
40
            $response = $callback($this->bookmarks);
41
            $results = $this->processResponse($response);
42
43
            if (empty($results)) return;
44
45
            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...
46
                $resultsNum++;
47
                yield $result;
48
49
                if ($this->paginationFinished($resultsNum)) {
50
                    return;
51
                }
52
            }
53
        }
54
55
        return;
56
    }
57
58
    /**
59
     * @param PaginatedResponse $response
60
     * @return array
61
     */
62
    protected function processResponse(PaginatedResponse $response)
63
    {
64
        if ($response->isEmpty()) return [];
65
66
        $this->bookmarks = $response->getBookmarks();
67
68
        return $response->getResponseData();
69
    }
70
71
    /**
72
     * @param int $resultsNum
73
     * @return bool
74
     */
75
    protected function paginationFinished($resultsNum)
76
    {
77
        return $this->reachesLimit($this->limit, $resultsNum) || $this->checkEndBookMarks();
78
    }
79
80
    /**
81
     * Check if we get results limit in pagination.
82
     *
83
     * @param int $limit
84
     * @param int $resultsNum
85
     *
86
     * @return bool
87
     */
88
    protected function reachesLimit($limit, $resultsNum)
89
    {
90
        return $limit && $resultsNum >= $limit;
91
    }
92
93
94
    /**
95
     * Checks for -end- substring in bookmarks. This is pinterest sign of
96
     * the finished pagination.
97
     *
98
     * @return bool
99
     */
100
    protected function checkEndBookMarks()
101
    {
102
        return !empty($this->bookmarks) && $this->bookmarks[0] == '-end-';
103
    }
104
}
105