Completed
Push — master ( 2f0a37...277b55 )
by Daniel
9s
created

GridOptions   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 6
loc 80
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 6 33 3
A getCurrentPage() 0 4 1
A getPageSize() 0 4 1
A getPageOffset() 0 4 1
A getOrderings() 0 4 1
A getVariant() 0 4 1
A getFilter() 0 4 1
A getFilterActionOptions() 0 6 1
A getOptions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class GridOptions
8
{
9
    private $options;
10
11
    public function __construct(array $options)
12
    {
13
        $defaults = [
14
            'page_size' => 50,
15
            'current_page' => 0,
16
            'orderings' => [],
17
            'filter' => [],
18
            'variant' => null,
19
        ];
20
21 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...
22
            throw new \InvalidArgumentException(sprintf(
23
                'Invalid grid options "%s". Valid options: "%s"',
24
                implode('", "', $diff), implode('", "', array_keys($defaults))
25
            ));
26
        }
27
28
        $options = array_merge($defaults, $options);
29
        $options['orderings'] = array_map(function ($order) {
30
            $order = strtolower($order);
31
32
            if (false === in_array($order, ['asc', 'desc'])) {
33
                throw new \InvalidArgumentException(sprintf(
34
                    'Order must be either "asc" or "desc" got "%s"',
35
                    $order
36
                ));
37
            }
38
39
            return $order;
40
        }, $options['orderings']);
41
42
        $this->options = $options;
43
    }
44
45
    public function getCurrentPage(): int
46
    {
47
        return $this->options['current_page'];
48
    }
49
50
    public function getPageSize(): int
51
    {
52
        return $this->options['page_size'];
53
    }
54
55
    public function getPageOffset(): int
56
    {
57
        return $this->getCurrentPage() * $this->getPageSize();
58
    }
59
60
    public function getOrderings(): array
61
    {
62
        return $this->options['orderings'];
63
    }
64
65
    public function getVariant()
66
    {
67
        return $this->options['variant'];
68
    }
69
70
    public function getFilter()
71
    {
72
        return $this->options['filter'];
73
    }
74
75
    public function getFilterActionOptions(): array
76
    {
77
        return array_filter($this->options, function ($key) {
78
            return $key !== 'filter';
79
        }, ARRAY_FILTER_USE_KEY);
80
    }
81
82
    public function getOptions(): array
83
    {
84
        return $this->options;
85
    }
86
}
87