TranslatedView   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

14 Methods

Rating   Name   Duplication   Size   Complexity  
previousMessageOption() 0 1 ?
nextMessageOption() 0 1 ?
buildPreviousMessage() 0 1 ?
buildNextMessage() 0 1 ?
A __construct() 0 5 1
A render() 0 6 1
A addTranslationOptions() 0 6 1
A addPreviousTranslationOption() 0 7 1
A addNextTranslationOption() 0 7 1
A addTranslationOption() 0 10 2
A previousMessage() 0 6 1
A nextMessage() 0 6 1
A previousText() 0 4 1
A nextText() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Pagerfanta package.
5
 *
6
 * (c) Pablo Díez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WhiteOctober\PagerfantaBundle\View;
13
14
use Pagerfanta\PagerfantaInterface;
15
use Pagerfanta\View\ViewInterface;
16
use Symfony\Component\Translation\TranslatorInterface;
17
18
/**
19
 * Translated view.
20
 *
21
 * @author Pablo Díez <[email protected]>
22
 */
23
abstract class TranslatedView implements ViewInterface
24
{
25
    private $view;
26
    private $translator;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param ViewInterface       $view       A view.
32
     * @param TranslatorInterface $translator A translator interface.
33
     */
34
    public function __construct(ViewInterface $view, TranslatorInterface $translator)
35
    {
36
        $this->view = $view;
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array())
44
    {
45
        $optionsWithTranslations = $this->addTranslationOptions($options);
46
47
        return $this->view->render($pagerfanta, $routeGenerator, $optionsWithTranslations);
48
    }
49
50
    private function addTranslationOptions($options)
51
    {
52
        return $this->addNextTranslationOption(
53
            $this->addPreviousTranslationOption($options)
54
        );
55
    }
56
57
    private function addPreviousTranslationOption($options)
58
    {
59
        $option = $this->previousMessageOption();
60
        $messageMethod = 'previousMessage';
61
62
        return $this->addTranslationOption($options, $option, $messageMethod);
63
    }
64
65
    private function addNextTranslationOption($options)
66
    {
67
        $option = $this->nextMessageOption();
68
        $messageMethod = 'nextMessage';
69
70
        return $this->addTranslationOption($options, $option, $messageMethod);
71
    }
72
73
    private function addTranslationOption($options, $option, $messageMethod)
74
    {
75
        if (isset($options[$option])) {
76
            return $options;
77
        }
78
79
        $message = $this->$messageMethod();
80
81
        return array_merge($options, array($option => $message));
82
    }
83
84
    abstract protected function previousMessageOption();
85
86
    abstract protected function nextMessageOption();
87
88
    private function previousMessage()
89
    {
90
        $previousText = $this->previousText();
91
92
        return $this->buildPreviousMessage($previousText);
93
    }
94
95
    private function nextMessage()
96
    {
97
        $nextText = $this->nextText();
98
99
        return $this->buildNextMessage($nextText);
100
    }
101
102
    private function previousText()
103
    {
104
        return $this->translator->trans('previous', array(), 'pagerfanta');
105
    }
106
107
    private function nextText()
108
    {
109
        return $this->translator->trans('next', array(), 'pagerfanta');
110
    }
111
112
    abstract protected function buildPreviousMessage($text);
113
114
    abstract protected function buildNextMessage($text);
115
}
116