Completed
Pull Request — master (#78)
by Wouter
02:12
created

TranslatableAdminExtension::preUpdate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata 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();
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 ($this->getTranslatableChecker()->isTranslatable($object) && ($unitOfWork->getCurrentLocale($object) != $locale)) {
43
            $object = $this->getDocumentManager($admin)->findTranslation($admin->getClass(), $object->getId(), $locale);
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function preUpdate(AdminInterface $admin, $object)
51
    {
52
        $locale          = $this->getTranslatableLocale($admin);
53
        $documentManager = $this->getDocumentManager($admin);
54
        $unitOfWork      = $documentManager->getUnitOfWork();
55
56
        if ($this->getTranslatableChecker()->isTranslatable($object) && ($unitOfWork->getCurrentLocale($object) != $locale)) {
57
            $documentManager->bindTranslation($object, $locale);
58
        }
59
    }
60
}
61