Completed
Pull Request — master (#15)
by Daniel
01:58
created

GridContext   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

9 Methods

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