AbstractTranslatableAdminExtension   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 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
A getContainer() 0 4 1
A getTranslationLocales() 0 4 1
A getDefaultTranslationLocale() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\TranslationBundle\Admin\Extension;
15
16
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\TranslationBundle\Checker\TranslatableChecker;
19
20
/**
21
 * @author Nicolas Bastien <[email protected]>
22
 */
23
abstract class AbstractTranslatableAdminExtension extends AbstractAdminExtension
24
{
25
    /**
26
     * Request parameter.
27
     */
28
    public const TRANSLATABLE_LOCALE_PARAMETER = 'tl';
29
30
    /**
31
     * @var string
32
     */
33
    protected $translatableLocale;
34
35
    /**
36
     * @var TranslatableChecker
37
     */
38
    protected $translatableChecker;
39
40
    public function __construct(TranslatableChecker $translatableChecker)
41
    {
42
        $this->translatableChecker = $translatableChecker;
43
    }
44
45
    /**
46
     * @param TranslatableChecker $translatableChecker
47
     */
48
    public function setTranslatableChecker($translatableChecker): void
49
    {
50
        $this->translatableChecker = $translatableChecker;
51
    }
52
53
    /**
54
     * @return TranslatableChecker
55
     */
56
    public function getTranslatableChecker()
57
    {
58
        return $this->translatableChecker;
59
    }
60
61
    /**
62
     * Return current translatable locale
63
     * ie: the locale used to load object translations != current request locale.
64
     *
65
     * @return string
66
     */
67
    public function getTranslatableLocale(AdminInterface $admin)
68
    {
69
        if (null === $this->translatableLocale) {
70
            if ($admin->hasRequest()) {
71
                $this->translatableLocale = $admin->getRequest()->get(self::TRANSLATABLE_LOCALE_PARAMETER);
72
            }
73
            if (null === $this->translatableLocale) {
74
                $this->translatableLocale = $this->getDefaultTranslationLocale($admin);
75
            }
76
        }
77
78
        return $this->translatableLocale;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getPersistentParameters(AdminInterface $admin)
85
    {
86
        return [self::TRANSLATABLE_LOCALE_PARAMETER => $this->getTranslatableLocale($admin)];
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function alterNewInstance(AdminInterface $admin, $object): void
93
    {
94
        if (null === $object->getLocale()) {
95
            $object->setLocale($this->getTranslatableLocale($admin));
96
        }
97
    }
98
99
    /**
100
     * @return ContainerInterface
101
     */
102
    protected function getContainer(AdminInterface $admin)
103
    {
104
        return $admin->getConfigurationPool()->getContainer();
105
    }
106
107
    /**
108
     * Return the list of possible locales for your models.
109
     *
110
     * @return array
111
     */
112
    protected function getTranslationLocales(AdminInterface $admin)
113
    {
114
        return $this->getContainer($admin)->getParameter('sonata_translation.locales');
115
    }
116
117
    /**
118
     * Return the default locale if url parameter is not present.
119
     *
120
     * @return string
121
     */
122
    protected function getDefaultTranslationLocale(AdminInterface $admin)
123
    {
124
        return $this->getContainer($admin)->getParameter('sonata_translation.default_locale');
125
    }
126
}
127