Completed
Pull Request — master (#15)
by Daniel
02:00
created

Header   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A isSorted() 0 5 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
        return isset($ordering[$this->name]);
27
    }
28
29
    public function isSortAscending(): bool
30
    {
31
        if (false === $this->isSorted()) {
32
            throw new \RuntimeException(sprintf(
33
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
34
                $this->name
35
            ));
36
        }
37
38
        return $this->options->getOrderings()[$this->name] === 'asc';
39
    }
40
41
    public function getUrlParametersForSort($order = 'asc')
42
    {
43
        $options = $this->options->getUrlParameters();
44
        $options['orderings'] = [
45
            $this->name => $order
46
        ];
47
48
        return $options;
49
    }
50
}
51