Completed
Pull Request — master (#180)
by Grégoire
12:29
created

TranslatableAdminExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\Phpcr;
13
14
use Doctrine\ODM\PHPCR\DocumentManager;
15
use Doctrine\ODM\PHPCR\Translation\LocaleChooser\LocaleChooser;
16
use Sonata\AdminBundle\Admin\AdminInterface;
17
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
18
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
19
use Sonata\TranslationBundle\Checker\TranslatableChecker;
20
21
/**
22
 * @author Nicolas Bastien <[email protected]>
23
 */
24
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension
25
{
26
    /**
27
     * @var LocaleChooser
28
     */
29
    private $localeChooser;
30
31
    public function __construct(TranslatableChecker $translatableChecker, LocaleChooser $localeChooser)
32
    {
33
        parent::__construct($translatableChecker);
34
        $this->localeChooser = $localeChooser;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function alterObject(AdminInterface $admin, $object)
41
    {
42
        $locale = $this->getTranslatableLocale($admin);
43
        $documentManager = $this->getDocumentManager($admin);
44
        $unitOfWork = $documentManager->getUnitOfWork();
45
46
        if (
47
            $this->getTranslatableChecker()->isTranslatable($object)
48
            && ($unitOfWork->getCurrentLocale($object) !== $locale)
49
        ) {
50
            $object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale);
51
52
            // if the translation did not yet exists, the locale will be
53
            // the fallback locale. This makes sure the new locale is set.
54
            if ($unitOfWork->getCurrentLocale($object) !== $locale) {
55
                $documentManager->bindTranslation($object, $locale);
56
            }
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
64
    {
65
        $this->localeChooser->setLocale($this->getTranslatableLocale($admin));
66
    }
67
68
    /**
69
     * @param AdminInterface $admin
70
     *
71
     * @return DocumentManager
72
     */
73
    protected function getDocumentManager(AdminInterface $admin)
74
    {
75
        return $admin->getModelManager()->getDocumentManager();
0 ignored issues
show
Bug introduced by
The method getDocumentManager() does not seem to exist on object<Sonata\AdminBundl...\ModelManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
    }
77
}
78