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

Header   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A isSorted() 0 6 1
A isSortAscending() 0 11 2
A getUrlParametersForSort() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
class Header
8
{
9
    private $name;
10
    private $options;
11
12
    public function __construct(string $name, GridContext $options)
13
    {
14
        $this->name = $name;
15
        $this->options = $options;
16
    }
17
18
    public function getName()
19
    {
20
        return $this->name;
21
    }
22
23
    public function isSorted(): bool
24
    {
25
        $ordering = $this->options->getOrderings();
26
27
        return isset($ordering[$this->name]);
28
    }
29
30
    public function isSortAscending(): bool
31
    {
32
        if (false === $this->isSorted()) {
33
            throw new \RuntimeException(sprintf(
34
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
35
                $this->name
36
            ));
37
        }
38
39
        return $this->options->getOrderings()[$this->name] === 'asc';
40
    }
41
42
    public function getUrlParametersForSort($order = 'asc')
43
    {
44
        $options = $this->options->getUrlParameters();
45
        $options['orderings'] = [
46
            $this->name => $order,
47
        ];
48
49
        return $options;
50
    }
51
}
52