Completed
Pull Request — master (#29)
by Daniel
02:17
created

Header   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 57
rs 10
c 0
b 0
f 0

7 Methods

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