Completed
Pull Request — master (#5)
by Daniel
09:32 queued 07:20
created

GridOptions::getPageOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class GridOptions
8
{
9
    private $variant;
10
    private $page;
11
    private $pageSize;
12
    private $orderings;
13
14
    public function __construct(array $options)
15
    {
16
        $defaults = [
17
            'page_size' => 50,
18
            'current_page' => 0,
19
            'orderings' => [],
20
            'variant' => null,
21
        ];
22
23 View Code Duplication
        if ($diff = array_diff(array_keys($options), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
            throw new \InvalidArgumentException(sprintf(
25
                'Invalid grid options "%s". Valid options: "%s"',
26
                implode('", "', $diff), implode('", "', array_keys($defaults))
27
            ));
28
        }
29
30
        $options = array_merge($defaults, $options);
31
32
        $this->currentPage = $options['current_page'];
0 ignored issues
show
Bug introduced by
The property currentPage does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
        $this->pageSize = $options['page_size'];
34
        $this->orderings = $options['orderings'];
35
        $this->variant = $options['variant'];
36
    }
37
38
    public function getCurrentPage(): int
39
    {
40
        return $this->currentPage;
41
    }
42
43
    public function getPageSize(): int
44
    {
45
        return $this->pageSize;
46
    }
47
48
    public function getPageOffset(): int
49
    {
50
        return $this->currentPage * $this->pageSize;
51
    }
52
53
    public function getOrderings(): array
54
    {
55
        return $this->orderings;
56
    }
57
58
    public function getVariant()
59
    {
60
        return $this->variant;
61
    }
62
}
63