Completed
Pull Request — master (#25)
by
unknown
04:55
created

PaginationExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 10 1
B init() 0 29 5
A getName() 0 4 1
1
<?php
2
3
namespace Azine\EmailBundle\Services\Twig;
4
5
use Azine\EmailBundle\Services\Pagination;
6
use Azine\EmailBundle\Services\PaginationView;
7
8
class PaginationExtension extends \Twig_Extension
9
{
10
    /**
11
     * @var PaginationView
12
     */
13
    private $paginationView;
14
15
    /**
16
     * PaginationExtension constructor.
17
     *
18
     * @param PaginationView $paginationView
19
     */
20
    public function __construct(PaginationView $paginationView)
21
    {
22
        $this->paginationView = $paginationView;
23
    }
24
25
    public function getFunctions()
26
    {
27
        return [
28
            new \Twig_SimpleFunction(
29
                'gridPagination',
30
                [$this, 'init'],
31
                ['is_safe' => ['html']]
32
            ),
33
        ];
34
    }
35
36
    /**
37
     * Render pagination block.
38
     *
39
     * @param Pagination $pagination instance of Pagination class
40
     * @param array $paginationOptions list of PaginationView class options
41
     *
42
     * @return string
43
     * @throws \Exception
44
     */
45
    public function init(Pagination $pagination, array $paginationOptions = [])
46
    {
47
        $this->paginationView->setPagination($pagination);
48
49
        foreach ($paginationOptions as $optionName => $value) {
50
51
            $paginationView = new \ReflectionObject($this->paginationView);
52
53
            try {
54
                if ($paginationView->getProperty($optionName)->isPublic()) {
55
                    $this->paginationView->$optionName = $value;
56
57
                    continue;
58
                }
59
            } catch (\Exception $a) {
60
                throw new \Exception(
61
                    $a->getMessage() . ' in ' . PaginationView::class
62
                );
63
            }
64
65
            $setterMethodName = 'set' . ucfirst($optionName);
66
67
            if ($paginationView->hasMethod($setterMethodName)) {
68
                $this->paginationView->$setterMethodName($value);
69
            }
70
        }
71
72
        return $this->paginationView->renderPageButtons();
73
    }
74
75
    public function getName()
76
    {
77
        return get_class($this);
78
    }
79
}