Completed
Push — master ( bd326e...3b0d07 )
by Paweł
12:10 queued 06:38
created

PaginationDecorator::addPagination()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 9
nc 5
nop 2
1
<?php
2
3
/**
4
 * This file is part of the PHP SDK library for the Superdesk Content API.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú.
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace Superdesk\ContentApiSdk\Api\Request;
16
17
use Superdesk\ContentApiSdk\Exception\InvalidArgumentException;
18
use Superdesk\ContentApiSdk\Exception\RequestException;
19
20
/**
21
 * Pagination decorator for API request.
22
 */
23
class PaginationDecorator extends RequestDecorator
24
{
25
    /**
26
     * Sets page number and max results per page parameters.
27
     *
28
     * @param int $offset Offset of rows
29
     * @param int $length Length of rows
30
     *
31
     * @return self
32
     */
33
    public function addPagination($offset, $length)
34
    {
35
        try {
36
            $parameters = $this->getParameters();
37
            $parameters->setPage((int) (ceil($offset / $length) + 1));
38
            $parameters->setMaxResults((int) $length);
39
            $this->setParameters($parameters);
40
        } catch (InvalidArgumentException $e) {
41
            throw new RequestException('Could not set pagination parameters.');
42
        }
43
44
        return $this;
45
    }
46
}
47