ElasticaSorterExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
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