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

TranslatableAdminExtension::alterObject()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
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
     * @param AdminInterface $admin
25
     *
26
     * @return DocumentManager
27
     */
28
    protected function getDocumentManager(AdminInterface $admin)
29
    {
30
        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...
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function alterObject(AdminInterface $admin, $object)
37
    {
38
        $locale = $this->getTranslatableLocale($admin);
39
        $documentManager = $this->getDocumentManager($admin);
40
        $unitOfWork = $documentManager->getUnitOfWork();
41
42
        if (
43
            $this->getTranslatableChecker()->isTranslatable($object)
44
            && ($unitOfWork->getCurrentLocale($object) !== $locale)
45
        ) {
46
            $object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale);
47
48
            // if the translation did not yet exists, the locale will be
49
            // the fallback locale. This makes sure the new locale is set.
50
            if ($unitOfWork->getCurrentLocale($object) !== $locale) {
51
                $documentManager->bindTranslation($object, $locale);
52
            }
53
        }
54
    }
55
}
56