Completed
Pull Request — master (#13)
by ARCANEDEV
10:25
created

AutoPagingIterator::valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\Stripe\Utilities;
2
3
use Arcanedev\Stripe\Collection;
4
use Iterator;
5
6
/**
7
 * Class     AutoPagingIterator
8
 *
9
 * @package  Arcanedev\Stripe\Utilities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class AutoPagingIterator implements Iterator
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    private $lastId = null;
19
20
    /** @var \Arcanedev\Stripe\Collection  */
21
    private $page   = null;
22
23
    /** @var array */
24
    private $params = [];
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Constructor
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * AutoPagingIterator constructor.
32
     *
33
     * @param  \Arcanedev\Stripe\Collection  $collection
34
     * @param  array                         $params
35
     */
36 10
    public function __construct(Collection $collection, array $params)
37
    {
38 10
        $this->page   = $collection;
39 10
        $this->params = $params;
40 10
    }
41
42
    /**
43
     * Make a AutoPagingIterator object.
44
     *
45
     * @param  \Arcanedev\Stripe\Collection  $collection
46
     * @param  array                         $params
47
     *
48
     * @return self
49
     */
50 10
    public static function make(Collection $collection, array $params)
51
    {
52 10
        return new self($collection, $params);
53
    }
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  Main Functions
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * Return the current element.
61
     * @link  http://php.net/manual/en/iterator.current.php
62
     *
63
     * @return mixed
64
     */
65 10
    public function current()
66
    {
67 10
        $item         = current($this->page->data);
68 10
        $this->lastId = $item !== false ? $item['id'] : null;
69
70 10
        return $item;
71
    }
72
73
    /**
74
     * Move forward to next element.
75
     * @link  http://php.net/manual/en/iterator.next.php
76
     *
77
     * @return void
78
     */
79 10
    public function next()
80
    {
81 10
        $item = next($this->page->data);
82
83 10
        if ($item === false) {
84
            // If we've run out of data on the current page, try to fetch another one
85 10
            if ($this->page['has_more']) {
86 5
                $this->params = array_merge(
87 5
                    $this->params ? $this->params : [],
88 5
                    ['starting_after' => $this->lastId]
89 4
                );
90
91 5
                $this->page   = $this->page->all($this->params);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->page->all($this->params) of type object<Arcanedev\Stripe\StripeObject> or array is incompatible with the declared type object<Arcanedev\Stripe\Collection> of property $page.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
92
            }
93
            else {
94 5
                return false;
95
            }
96
        }
97 10
    }
98
99
    /**
100
     * Return the key of the current element.
101
     * @link  http://php.net/manual/en/iterator.key.php
102
     *
103
     * @return mixed
104
     */
105
    public function key()
106
    {
107
        return key($this->page->data);
108
    }
109
110
    /**
111
     * Checks if current position is valid.
112
     * @link  http://php.net/manual/en/iterator.valid.php
113
     *
114
     * @return bool
115
     */
116 10
    public function valid()
117
    {
118 10
        $key  = key($this->page->data);
119
120 10
        return ($key !== null && $key !== false);
121
    }
122
123
    /**
124
     * Rewind the Iterator to the first element.
125
     * @link  http://php.net/manual/en/iterator.rewind.php
126
     *
127
     * @return void
128
     */
129 10
    public function rewind()
130
    {
131
        // Actually rewinding would require making a copy of the original page.
132 10
    }
133
}
134