Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AdminList/TranslationAdminListConfigurator.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\AdminList;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineDBALAdminListConfigurator;
8
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\EnumerationFilterType;
9
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\StringFilterType;
10
use Kunstmaan\TranslatorBundle\Entity\Translation;
11
12
/**
13
 * TranslationAdminListConfigurator
14
 */
15
class TranslationAdminListConfigurator extends AbstractDoctrineDBALAdminListConfigurator
16
{
17
18
    /**
19
     * @var array $locales
20
     */
21
    protected $locales;
22
23
    /**
24
     * @var string
25
     */
26
    protected $locale;
27
28
    /**
29
     * @param Connection $connection
30
     * @param array $locales
31
     */
32
    public function __construct(Connection $connection, array $locales)
33
    {
34
        parent::__construct($connection);
35
        $this->locales = $locales;
36
        $this->setCountField('CONCAT(b.translation_id)');
37
    }
38
39
    /**
40
     * Configure filters
41
     */
42
    public function buildFilters()
43
    {
44
        $this->addFilter('status', new StringFilterType('status'), 'kuma_translator.adminlist.filter.status');
45
        $this->addFilter('domain', new StringFilterType('domain'), 'kuma_translator.adminlist.filter.domain');
46
        $this->addFilter('keyword', new StringFilterType('keyword'), 'kuma_translator.adminlist.filter.keyword');
47
        $this->addFilter('text', new StringFilterType('text'), 'kuma_translator.adminlist.filter.text');
48
        $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
49
            $this->locales, $this->locales
50
        ));
51
    }
52
53
    /**
54
     * Configure the visible columns
55
     */
56
    public function buildFields()
57
    {
58
        $this->addField('domain', 'kuma_translator.adminlist.header.domain', true);
59
        $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true);
60
        $this->addField('status', 'kuma_translator.adminlist.header.status', true);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function canAdd()
67
    {
68
        return true;
69
    }
70
71
    /**
72
     * @param object|array $item
73
     *
74
     * @return bool
75
     */
76
    public function canEdit($item)
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * @param object|array $item
83
     *
84
     * @return bool
85
     */
86
    public function canEditInline($item)
87
    {
88
        return true;
89
    }
90
91
    /**
92
     * Override path convention (because settings is a virtual admin subtree)
93
     *
94
     * @param string $suffix
95
     *
96
     * @return string
97
     */
98 View Code Duplication
    public function getPathByConvention($suffix = null)
99
    {
100
        if (empty($suffix)) {
101
            return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName()));
102
        }
103
104
        return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getAdminType($item)
111
    {
112
        return null;
113
    }
114
115
    public function getBundleName()
116
    {
117
        return 'KunstmaanTranslatorBundle';
118
    }
119
120
    public function getEntityName()
121
    {
122
        return 'Translation';
123
    }
124
125
    public function getControllerPath()
126
    {
127
        return 'KunstmaanTranslatorBundle:Index';
128
    }
129
130
    /**
131
     * @return QueryBuilder|null
132
     */
133
    public function getQueryBuilder()
134
    {
135
        if (is_null($this->queryBuilder)) {
136
            $this->queryBuilder = new QueryBuilder($this->connection);
137
            $this->queryBuilder
138
                ->select('DISTINCT b.translation_id AS id, b.keyword, b.domain, b.status')
139
                ->from('kuma_translation', 'b')
140
                ->andWhere('b.status != :statusstring')
141
                ->setParameter('statusstring', Translation::STATUS_DISABLED);
142
143
            // Apply filters
144
            $filters = $this->getFilterBuilder()->getCurrentFilters();
145
            $locales = array();
146
147
            $textValue = $textComparator = null;
148
            foreach ($filters as $filter) {
149
                if ($filter->getType() instanceof EnumerationFilterType && $filter->getColumnName() == 'locale') {
150
                    // Override default enumeration filter handling ... catch selected locales here
151
                    $data = $filter->getData();
152
                    $comparator = $filter->getType()->getComparator();
153
                    if ($comparator == 'in') {
154
                        $locales = $data['value'];
155
                    } elseif ($comparator == 'notin') {
156
                        $locales = array_diff($this->locales, $data['value']);
157
                    }
158
                } elseif ($filter->getType() instanceof StringFilterType && $filter->getColumnName() == 'text') {
159
                    // Override default text filter handling ...
160
                    $data =  $filter->getData();
161
                    $textValue = $data['value'];
162
                    $textComparator = $data['comparator'];
163
                } else {
164
                    /* @var AbstractDBALFilterType $type */
165
                    $type = $filter->getType();
166
                    $type->setQueryBuilder($this->queryBuilder);
167
                    $filter->apply();
168
                }
169
            }
170
171
            if (!empty($locales)) {
172
                $this->locales = $locales;
173
            }
174
175
            // Field building hack...
176
            foreach ($this->locales as $locale) {
177
                $this->addField($locale, strtoupper($locale), false, 'KunstmaanTranslatorBundle:Translator:inline_edit.html.twig');
178
            }
179
180
            // Field filter hack...
181
            $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
182
                $this->locales, $this->locales
183
            ));
184
185
            // Add join for every locale
186
            foreach ($this->locales as $locale) {
187
                $this->queryBuilder->addSelect('t_' . $locale . '.`text` AS ' . $locale);
188
                $this->queryBuilder->addSelect('t_' . $locale . '.id AS ' . $locale . '_id');
189
                $this->queryBuilder->leftJoin('b', 'kuma_translation', 't_' . $locale,
190
                  'b.keyword = t_' . $locale . '.keyword and b.domain = t_' . $locale . '.domain and t_' . $locale . '.locale=:locale_' . $locale);
191
                $this->queryBuilder->setParameter('locale_' . $locale, $locale);
192
            }
193
194
            // Apply text filter
195
            if (!is_null($textValue) && !is_null($textComparator)) {
196
                $orX = $this->queryBuilder->expr()->orX();
197
198
                foreach ($this->locales as $key => $locale) {
199
                    $uniqueId = 'txt_' . $key;
200
                    switch ($textComparator) {
201
                        case 'equals':
202
                            $expr = $this->queryBuilder->expr()->eq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
203
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
204
                            break;
205
                        case 'notequals':
206
                            $expr = $this->queryBuilder->expr()->neq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
207
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
208
                            break;
209 View Code Duplication
                        case 'contains':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
211
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
212
                            break;
213 View Code Duplication
                        case 'doesnotcontain':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
                            $expr = 't_' . $locale . '.`text`' . ' NOT LIKE :var_' . $uniqueId;
215
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
216
                            break;
217
                        case 'startswith':
218
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
219
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue . '%');
220
                            break;
221
                        case 'endswith':
222
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
223
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue);
224
                            break;
225
                    }
226
                    $orX->add($expr);
0 ignored issues
show
The variable $expr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
227
                }
228
229
                $this->queryBuilder->andWhere($orX);
230
            }
231
232
            // Apply sorting
233 View Code Duplication
            if (!empty($this->orderBy)) {
234
                $orderBy = $this->orderBy;
235
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
236
            }
237
        }
238
239
        return $this->queryBuilder;
240
    }
241
}
242