Completed
Push — master ( a9154f...38f43b )
by Daniel
02:17
created

Header   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getName() 0 4 1
A getLabel() 0 4 1
A getTemplate() 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 $label;
13
    private $template;
14
    private $gridContext;
15
    private $sortField;
16
17
    public function __construct(GridContext $gridContext, string $template, string $name, string $label, string $sortField = null)
18
    {
19
        $this->name = $name;
20
        $this->label = $label;
21
        $this->template = $template;
22
        $this->gridContext = $gridContext;
23
        $this->sortField = $sortField;
24
    }
25
26
    public function getName(): string
27
    {
28
        return $this->name;
29
    }
30
31
    public function getLabel(): string
32
    {
33
        return $this->label;
34
    }
35
36
    public function getTemplate(): string
37
    {
38
        return $this->template;
39
    }
40
41
    public function isSorted(): bool
42
    {
43
        $ordering = $this->gridContext->getOrderings();
44
45
        return isset($ordering[$this->name]);
46
    }
47
48
    public function isSortAscending(): bool
49
    {
50
        if (false === $this->isSorted()) {
51
            throw new \RuntimeException(sprintf(
52
                'Cannot determine if sort is ascending when the field ("%s") is not sorted.',
53
                $this->name
54
            ));
55
        }
56
57
        return $this->gridContext->getOrderings()[$this->name] === 'asc';
58
    }
59
60
    public function getSortField(): string
61
    {
62
        return $this->sortField;
63
    }
64
65
    public function canBeSorted(): bool
66
    {
67
        return null !== $this->sortField;
68
    }
69
70
    public function getUrlParametersForSort($order = 'asc'): array
71
    {
72
        $gridContext = $this->gridContext->getUrlParameters();
73
        $gridContext['orderings'] = [
74
            $this->name => $order,
75
        ];
76
77
        return $gridContext;
78
    }
79
}
80