Completed
Push — master ( 4aaab4...5f2b12 )
by
unknown
01:34 queued 10s
created

TranslatableAdminExtension::getDocumentManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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