Completed
Push — master ( d7e260...fc7aea )
by Artem
10:19
created

UserGroupsIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 83
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A current() 0 3 1
A requestsCount() 0 3 1
A __construct() 0 4 1
A next() 0 3 1
A valid() 0 3 1
A rewind() 0 3 1
1
<?php
2
3
namespace Slides\Connector\Auth\Sync;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class UserGroupsIterator
9
 *
10
 * @package Slides\Connector\Auth\Sync
11
 */
12
class UserGroupsIterator implements \Iterator
13
{
14
    /**
15
     * The users collection.
16
     *
17
     * @var Collection
18
     */
19
    protected $users;
20
21
    /**
22
     * Number of users which can be sent per request
23
     *
24
     * @var int
25
     */
26
    protected $perRequest;
27
28
    /**
29
     * The iterator position
30
     *
31
     * @var int
32
     */
33
    protected $position;
34
35
    /**
36
     * UserGroupsIterator constructor.
37
     *
38
     * @param Collection $users
39
     * @param int $perRequest
40
     */
41
    public function __construct(Collection $users, int $perRequest)
42
    {
43
        $this->users = $users;
44
        $this->perRequest = $perRequest;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function rewind()
51
    {
52
        $this->position = 1;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function next()
59
    {
60
        $this->position++;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function valid()
67
    {
68
        return $this->current()->isNotEmpty();
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function current()
75
    {
76
        return $this->users->forPage($this->position, $this->perRequest);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function key()
83
    {
84
        return $this->position;
85
    }
86
87
    /**
88
     * Calculate a number of requests.
89
     *
90
     * @return int
91
     */
92
    public function requestsCount(): int
93
    {
94
        return max(1, ceil($this->users->count() / $this->perRequest));
95
    }
96
}