Completed
Push — 2.x-dev-kit ( 4d92c6 )
by
unknown
19:25 queued 16:48
created

TranslatableAdminExtension::alterObject()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 0
loc 19
rs 9.2
cc 4
eloc 10
nc 3
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 Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\TranslationBundle\Admin\Extension\AbstractTranslatableAdminExtension;
17
18
/**
19
 * @author Nicolas Bastien <[email protected]>
20
 */
21
class TranslatableAdminExtension extends AbstractTranslatableAdminExtension
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function alterObject(AdminInterface $admin, $object)
27
    {
28
        $locale = $this->getTranslatableLocale($admin);
29
        $documentManager = $this->getDocumentManager($admin);
30
        $unitOfWork = $documentManager->getUnitOfWork();
31
32
        if (
33
            $this->getTranslatableChecker()->isTranslatable($object)
34
            && ($unitOfWork->getCurrentLocale($object) !== $locale)
35
        ) {
36
            $object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale);
37
38
            // if the translation did not yet exists, the locale will be
39
            // the fallback locale. This makes sure the new locale is set.
40
            if ($unitOfWork->getCurrentLocale($object) !== $locale) {
41
                $documentManager->bindTranslation($object, $locale);
42
            }
43
        }
44
    }
45
46
    /**
47
     * @param AdminInterface $admin
48
     *
49
     * @return DocumentManager
50
     */
51
    protected function getDocumentManager(AdminInterface $admin)
52
    {
53
        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...
54
    }
55
}
56