Completed
Push — master ( 70aa26...ed7488 )
by Daniel
04:57 queued 02:49
created

GridContext   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 6 48 6
A getCurrentPage() 0 4 1
A isPaginated() 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 getUrlParameters() 0 6 1
A getClassFqn() 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 GridContext
8
{
9
    /**
10
     * @var array
11
     */
12
    private $options;
13
14
    /**
15
     * @var string
16
     */
17
    private $classFqn;
18
19
    public function __construct(string $classFqn, array $options)
20
    {
21
        $this->classFqn = $classFqn;
22
        $defaults = [
23
            'page_size' => 50,
24
            'page' => 1,
25
            'orderings' => [],
26
            'filter' => [],
27
            'variant' => null,
28
        ];
29
30
        // check for invalid keys
31 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...
32
            throw new \InvalidArgumentException(sprintf(
33
                'Invalid grid context options "%s". Valid options: "%s"',
34
                implode('", "', $diff), implode('", "', array_keys($defaults))
35
            ));
36
        }
37
38
        // set defaults
39
        $options = array_merge($defaults, $options);
40
41
        // normalize the orderings
42
        $options['orderings'] = array_map(function ($order) {
43
            $order = strtolower($order);
44
45
            if (false === in_array($order, ['asc', 'desc'])) {
46
                throw new \InvalidArgumentException(sprintf(
47
                    'Order must be either "asc" or "desc" got "%s"',
48
                    $order
49
                ));
50
            }
51
52
            return $order;
53
        }, $options['orderings']);
54
55
        // cast integer values where applicable
56
        foreach (['page', 'page_size'] as $key) {
57
            $options[$key] = $options[$key] !== null ? (int) $options[$key] : null;
58
        }
59
60
        // ensure current page is > 0
61
        if ($options['page'] < 1) {
62
            $options['page'] = 1;
63
        }
64
65
        $this->options = $options;
66
    }
67
68
    public function getCurrentPage(): int
69
    {
70
        return $this->options['page'];
71
    }
72
73
    public function isPaginated(): bool
74
    {
75
        return null !== $this->options['page_size'];
76
    }
77
78
    public function getPageSize(): int
79
    {
80
        return $this->options['page_size'];
81
    }
82
83
    public function getPageOffset(): int
84
    {
85
        return ($this->getCurrentPage() - 1) * $this->getPageSize();
86
    }
87
88
    public function getOrderings(): array
89
    {
90
        return $this->options['orderings'];
91
    }
92
93
    public function getVariant()
94
    {
95
        return $this->options['variant'];
96
    }
97
98
    public function getFilter()
99
    {
100
        return $this->options['filter'];
101
    }
102
103
    public function getUrlParameters(): array
104
    {
105
        return array_merge([
106
            'class' => $this->classFqn,
107
        ], $this->options);
108
    }
109
110
    public function getClassFqn(): string
111
    {
112
        return $this->classFqn;
113
    }
114
}
115