Passed
Push — master ( c22b6a...5f79f5 )
by Julito
11:21
created

Pagination   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 53
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A loadItems() 0 26 4
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\Zoom\API;
6
7
use Exception;
8
9
/**
10
 * Trait Pagination
11
 * properties for Pagination objects, which are paginated lists of items,
12
 * retrieved in chunks from the server over one or several API calls, one per page.
13
 */
14
trait Pagination
15
{
16
    use JsonDeserializableTrait;
17
18
    /** @var int The number of pages returned for the request made. */
19
    public $page_count;
20
21
    /** @var int The page number of the current results, counting from 1 */
22
    public $page_number;
23
24
    /** @var int The number of records returned with a single API call. Default 30, max 300. */
25
    public $page_size;
26
27
    /** @var int The total number of all the records available across pages. */
28
    public $total_records;
29
30
    /**
31
     * Retrieves all items from the server, possibly generating several API calls.
32
     *
33
     * @param string $arrayPropertyName item array property name
34
     * @param string $relativePath      relative path to pass to Client::send
35
     * @param array  $parameters        parameter array to pass to Client::send
36
     *
37
     * @throws Exception
38
     *
39
     * @return array united list of items
40
     */
41
    protected static function loadItems($arrayPropertyName, $relativePath, $parameters = [])
42
    {
43
        $items = [];
44
        $pageCount = 1;
45
        $pageSize = 300;
46
        $totalRecords = 0;
47
        for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
48
            $response = static::fromJson(
49
                Client::getInstance()->send(
50
                    'GET',
51
                    $relativePath,
52
                    array_merge(['page_size' => $pageSize, 'page_number' => $pageNumber], $parameters)
53
                )
54
            );
55
            $items = array_merge($items, $response->$arrayPropertyName);
56
            if (0 === $totalRecords) {
57
                $pageCount = $response->page_count;
58
                $pageSize = $response->page_size;
59
                $totalRecords = $response->total_records;
60
            }
61
        }
62
        if (count($items) !== $totalRecords) {
63
            error_log('Zoom announced '.$totalRecords.' records but returned '.count($items));
64
        }
65
66
        return $items;
67
    }
68
}
69