Completed
Push — master ( 92c457...b8e8aa )
by Sébastien
12:27 queued 10:37
created

Paginator::__construct()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 9.2568
c 0
b 0
f 0
cc 5
nc 5
nop 3
crap 5.009
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * soluble-flexstore library
7
 *
8
 * @author    Vanvelthem Sébastien
9
 * @link      https://github.com/belgattitude/soluble-flexstore
10
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
11
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
12
 *
13
 */
14
15
namespace Soluble\FlexStore\Helper;
16
17
use Soluble\FlexStore\Exception;
18
use Zend\Paginator\Paginator as ZendPaginator;
19
use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface;
20
21
class Paginator extends ZendPaginator
22
{
23
    /**
24
     * Default set of scrolling.
25
     *
26
     * @var array<string, string>
27
     */
28
    protected $scrollingTypes = [
29
        'all' => 'Zend\Paginator\ScrollingStyle\All',
30
        'elastic' => 'Zend\Paginator\ScrollingStyle\Elastic',
31
        'jumping' => 'Zend\Paginator\ScrollingStyle\Jumping',
32
        'sliding' => 'Zend\Paginator\ScrollingStyle\Sliding',
33
    ];
34
35
    /**
36
     * @param int $totalRows
37
     * @param int $limit
38
     * @param int $offset
39
     *
40
     * @throws Exception\InvalidUsageException
41
     */
42 2
    public function __construct(int $totalRows, int $limit, int $offset = 0)
43
    {
44 2
        if ($limit < 0) {
45 1
            throw new Exception\InvalidUsageException(__METHOD__ . ' expects limit to be an integer greater than 0');
46
        }
47 2
        if ($totalRows < 0) {
48 1
            throw new Exception\InvalidUsageException(__METHOD__ . ' expects total rows to be an integer greater than 0');
49
        }
50 2
        if ($offset < 0) {
51 1
            throw new Exception\InvalidUsageException(__METHOD__ . ' expects offset to be an integer greater than 0');
52
        }
53
54 1
        if (class_exists('\Zend\Paginator\Adapter\NullFill')) {
55 1
            $adapter = new \Zend\Paginator\Adapter\NullFill($totalRows);
56
        } else {
57
            throw new Exception\RuntimeException(__METHOD__ . " Missing Zend\Paginator\Adapter.");
58
        }
59
60 1
        parent::__construct($adapter);
61 1
        $this->setItemCountPerPage($limit);
62 1
        $this->setCurrentPageNumber(ceil(($offset + 1) / $limit));
63 1
    }
64
65
    /**
66
     * Loads a scrolling style.
67
     *
68
     * @param string|ScrollingStyleInterface $scrollingStyle
69
     *
70
     * @return ScrollingStyleInterface
71
     *
72
     * @throws Exception\InvalidArgumentException
73
     */
74
    protected function _loadScrollingStyle($scrollingStyle = null): ScrollingStyleInterface
75
    {
76
        if ($scrollingStyle === null) {
77
            $scrollingStyle = static::$defaultScrollingStyle;
78
        }
79
80
        if (is_string($scrollingStyle)) {
81
            if (!array_key_exists(strtolower($scrollingStyle), $this->scrollingTypes)) {
82
                throw new Exception\InvalidArgumentException(
83
                    "Scrolling type '$scrollingStyle' is not supported, look for (" .
84
                    implode(',', array_keys($this->scrollingTypes)) .
85
                    ')'
86
                );
87
            }
88
            $cls = $this->scrollingTypes[strtolower($scrollingStyle)];
89
90
            return new $cls();
91
        }
92
93
        if (!$scrollingStyle instanceof ScrollingStyleInterface) {
0 ignored issues
show
Bug introduced by
The class Zend\Paginator\Scrolling...ScrollingStyleInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
94
            throw new Exception\InvalidArgumentException(
95
                'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface'
96
            );
97
        }
98
99
        return $scrollingStyle;
100
    }
101
}
102