ElasticaSorterExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 47
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 13 1
A clearSort() 0 4 1
A displaySort() 0 11 1
A getName() 0 4 1
1
<?php
2
3
namespace Alpixel\Bundle\ElasticaQuerySorterBundle\Twig\Extension;
4
5
use Alpixel\Bundle\ElasticaQuerySorterBundle\Services\ElasticaQuerySorter;
6
7
class ElasticaSorterExtension extends \Twig_Extension
8
{
9
    protected $sorter;
10
    protected $configuration;
11
12
    public function __construct(ElasticaQuerySorter $sorter, $configuration)
13
    {
14
        $this->sorter = $sorter;
15
        $this->configuration = $configuration;
16
    }
17
18
    public function getFunctions()
19
    {
20
        return [
21
            new \Twig_SimpleFunction('elastica_sort', [$this, 'displaySort'], [
22
                'is_safe'           => ['html'],
23
                'needs_environment' => true,
24
            ]),
25
            new \Twig_SimpleFunction('elastica_clear_sort', [$this, 'clearSort'], [
26
                'is_safe'           => ['html'],
27
                'needs_environment' => true,
28
            ]),
29
        ];
30
    }
31
32
    public function clearSort(\Twig_Environment $twig)
33
    {
34
        return $twig->render($this->configuration['clear_sort']);
35
    }
36
37
    public function displaySort(\Twig_Environment $twig, $label, $sortKey)
38
    {
39
        $isCurrentSort = ($this->sorter->fetchData('sortBy') == $sortKey);
40
41
        return $twig->render($this->configuration['sort_link'], [
42
            'label'     => $label,
43
            'isCurrent' => $isCurrentSort,
44
            'sortKey'   => $sortKey,
45
            'sortOrder' => $this->sorter->fetchData('sortOrder'),
46
        ]);
47
    }
48
49
    public function getName()
50
    {
51
        return 'alpixel_elastica_sorter_extension';
52
    }
53
}
54