PaginationDecorator   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A addPagination() 0 13 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