Completed
Pull Request — master (#83)
by Matthieu
02:57
created

TranslatableAdminExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 2 Features 4
Metric Value
wmc 11
c 7
b 2
f 4
lcom 1
cbo 3
dl 0
loc 94
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTranslatableListener() 0 4 1
A alterObject() 0 10 2
A configureQuery() 0 5 1
B translationFieldFilter() 0 36 6
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\Gedmo;
13
14
use Gedmo\Translatable\TranslatableListener;
15
use Sonata\AdminBundle\Admin\AdminInterface;
16
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
17
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
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 TranslatableListener
28
     */
29
    protected $translatableListener;
30
31
    public function __construct(TranslatableChecker $translatableChecker, TranslatableListener $translatableListener)
32
    {
33
        parent::__construct($translatableChecker);
34
        $this->translatableListener = $translatableListener;
35
    }
36
37
    /**
38
     * @param AdminInterface $admin
0 ignored issues
show
Bug introduced by
There is no parameter named $admin. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     *
40
     * @return TranslatableListener
41
     */
42
    protected function getTranslatableListener()
43
    {
44
        return $this->translatableListener;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function alterObject(AdminInterface $admin, $object)
51
    {
52
        if ($this->getTranslatableChecker()->isTranslatable($object)) {
53
            $this->getTranslatableListener()->setTranslatableLocale($this->getTranslatableLocale($admin));
54
            $this->getTranslatableListener()->setTranslationFallback('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
56
            $this->getContainer($admin)->get('doctrine')->getManager()->refresh($object);
57
            $object->setLocale($this->getTranslatableLocale($admin));
58
        }
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
65
    {
66
        $this->getTranslatableListener()->setTranslatableLocale($this->getTranslatableLocale($admin));
67
        $this->getTranslatableListener()->setTranslationFallback('');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    /**
71
     * Search on normal field and on translation field
72
     * To use with a doctrine_orm_callback filter type.
73
     *
74
     * @param ProxyQuery $queryBuilder
75
     * @param string     $alias
76
     * @param string     $field
77
     * @param string     $value
78
     *
79
     * @return bool
80
     */
81
    public static function translationFieldFilter(ProxyQuery $queryBuilder, $alias, $field, $value)
82
    {
83
        if (!$value['value']) {
84
            return;
85
        }
86
87
        // verify if the join is not already done
88
        $aliasAlreadyExists = false;
89
        $joinDqlParts = $queryBuilder->getDQLParts()['join'];
90
        foreach ($joinDqlParts as $joins) {
91
            foreach ($joins as $join) {
92
                if ($join->getAlias() === 't') {
93
                    $aliasAlreadyExists = true;
94
                    break 2;
95
                }
96
            }
97
        }
98
99
        if ($aliasAlreadyExists === false) {
100
            $queryBuilder->leftJoin($alias.'.translations', 't');
101
        }
102
103
        // search on translation OR on normal field
104
        $queryBuilder->andWhere($queryBuilder->expr()->orX(
105
            $queryBuilder->expr()->andX(
106
                $queryBuilder->expr()->eq('t.field', $queryBuilder->expr()->literal($field)),
107
                $queryBuilder->expr()->like('t.content', $queryBuilder->expr()->literal('%'.$value['value'].'%'))
108
            ),
109
            $queryBuilder->expr()->like(
110
                sprintf('%s.%s', $alias, $field),
111
                $queryBuilder->expr()->literal('%'.$value['value'].'%')
112
            )
113
        ));
114
115
        return true;
116
    }
117
}
118