Completed
Push — master ( d17154...1fc820 )
by Sullivan
11:02 queued 08:59
created

AbstractTranslatableAdminExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 13
c 7
b 1
f 2
lcom 1
cbo 3
dl 0
loc 107
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getContainer() 0 4 1
A getTranslationLocales() 0 4 1
A getDefaultTranslationLocale() 0 4 1
A setTranslatableChecker() 0 4 1
A getTranslatableChecker() 0 4 1
A getTranslatableLocale() 0 13 4
A getPersistentParameters() 0 4 1
A alterNewInstance() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\TranslationBundle\Admin\Extension;
13
14
use Sonata\AdminBundle\Admin\AdminExtension;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\TranslationBundle\Checker\TranslatableChecker;
17
18
/**
19
 * @author Nicolas Bastien <[email protected]>
20
 */
21
abstract class AbstractTranslatableAdminExtension extends AdminExtension
0 ignored issues
show
Deprecated Code introduced by
The class Sonata\AdminBundle\Admin\AdminExtension has been deprecated with message: since version 3.1, to be removed in 4.0. Use Sonata\AdminBundle\AbstractAdminExtension instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * Request parameter.
25
     */
26
    const TRANSLATABLE_LOCALE_PARAMETER = 'tl';
27
28
    /**
29
     * @var string
30
     */
31
    protected $translatableLocale;
32
33
    /**
34
     * @var TranslatableChecker
35
     */
36
    protected $translatableChecker;
37
38
    /**
39
     * @param TranslatableChecker $translatableChecker
40
     */
41
    public function __construct(TranslatableChecker $translatableChecker)
42
    {
43
        $this->translatableChecker = $translatableChecker;
44
    }
45
46
    /**
47
     * @return ContainerInterface
48
     */
49
    protected function getContainer(AdminInterface $admin)
50
    {
51
        return $admin->getConfigurationPool()->getContainer();
52
    }
53
54
    /**
55
     * Return the list of possible locales for your models.
56
     *
57
     * @return array
58
     */
59
    protected function getTranslationLocales(AdminInterface $admin)
60
    {
61
        return $this->getContainer($admin)->getParameter('sonata_translation.locales');
62
    }
63
64
    /**
65
     * Return the default locale if url parameter is not present.
66
     *
67
     * @return string
68
     */
69
    protected function getDefaultTranslationLocale(AdminInterface $admin)
70
    {
71
        return $this->getContainer($admin)->getParameter('sonata_translation.default_locale');
72
    }
73
74
    /**
75
     * @param TranslatableChecker $translatableChecker
76
     */
77
    public function setTranslatableChecker($translatableChecker)
78
    {
79
        $this->translatableChecker = $translatableChecker;
80
    }
81
82
    /**
83
     * @return TranslatableChecker
84
     */
85
    public function getTranslatableChecker()
86
    {
87
        return $this->translatableChecker;
88
    }
89
90
    /**
91
     * Return current translatable locale
92
     * ie: the locale used to load object translations != current request locale.
93
     *
94
     * @return string
95
     */
96
    public function getTranslatableLocale(AdminInterface $admin)
97
    {
98
        if ($this->translatableLocale == null) {
99
            if ($admin->getRequest()) {
100
                $this->translatableLocale = $admin->getRequest()->get(self::TRANSLATABLE_LOCALE_PARAMETER);
101
            }
102
            if ($this->translatableLocale == null) {
103
                $this->translatableLocale = $this->getDefaultTranslationLocale($admin);
104
            }
105
        }
106
107
        return $this->translatableLocale;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getPersistentParameters(AdminInterface $admin)
114
    {
115
        return array(self::TRANSLATABLE_LOCALE_PARAMETER => $this->getTranslatableLocale($admin));
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function alterNewInstance(AdminInterface $admin, $object)
122
    {
123
        if ($object->getLocale() === null) {
124
            $object->setLocale($this->getTranslatableLocale($admin));
125
        }
126
    }
127
}
128