Completed
Push — master ( 16139d...720e1c )
by Jan
04:22
created

PartsDataTable::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 63
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 88
rs 8.8072

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * part-db version 0.1
4
 * Copyright (C) 2005 Christoph Lechner
5
 * http://www.cl-projects.de/.
6
 *
7
 * part-db version 0.2+
8
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
9
 * http://code.google.com/p/part-db/
10
 *
11
 * Part-DB Version 0.4+
12
 * Copyright (C) 2016 - 2019 Jan Böhmer
13
 * https://github.com/jbtronics
14
 *
15
 * This program is free software; you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License
17
 * as published by the Free Software Foundation; either version 2
18
 * of the License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
28
 */
29
30
namespace App\DataTables;
31
32
use App\Entity\Parts\Category;
33
use App\Entity\Parts\Part;
34
use App\Services\EntityURLGenerator;
35
use Doctrine\ORM\QueryBuilder;
36
use Omines\DataTablesBundle\Adapter\Doctrine\ORM\SearchCriteriaProvider;
37
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
38
use Omines\DataTablesBundle\Column\BoolColumn;
39
use Omines\DataTablesBundle\Column\DateTimeColumn;
40
use Omines\DataTablesBundle\Column\MapColumn;
41
use Omines\DataTablesBundle\Column\TextColumn;
42
use Omines\DataTablesBundle\DataTable;
43
use Omines\DataTablesBundle\DataTableTypeInterface;
44
use Symfony\Contracts\Translation\TranslatorInterface;
45
46
class PartsDataTable implements DataTableTypeInterface
47
{
48
    /**
49
     * @var EntityURLGenerator
50
     */
51
    protected $urlGenerator;
52
    protected $translator;
53
54
    public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator)
55
    {
56
        $this->urlGenerator = $urlGenerator;
57
        $this->translator = $translator;
58
    }
59
60
    protected function buildCriteria(QueryBuilder $builder, array $options)
61
    {
62
        if (isset($options['category'])) {
63
            $em = $builder->getEntityManager();
64
            $category = $options['category'];
65
            $repo = $em->getRepository(Category::class);
66
            $list = $repo->toNodesList($category);
67
            $list[] = $category;
68
69
            $builder->andWhere('part.category IN (:cid)')->setParameter('cid', $list);
70
        }
71
72
        if (isset($options['tag'])) {
73
            $builder->andWhere('part.tags LIKE :tag')->setParameter('tag', '%' . $options['tag'] . '%');
74
        }
75
76
        if (isset($options['search'])) {
77
            $builder->AndWhere('part.name LIKE :search')->orWhere('part.description LIKE :search')->orWhere('part.comment LIKE :search')
78
                ->setParameter('search', '%' . $options['search'] . '%');
79
        }
80
    }
81
82
    /**
83
     * @param DataTable $dataTable
84
     * @param array     $options
85
     */
86
    public function configure(DataTable $dataTable, array $options)
87
    {
88
        $dataTable
89
            ->add('name', TextColumn::class, [
90
                'label' => $this->translator->trans('part.table.name'),
91
                'render' => function ($value, Part $context) {
92
                    return $this->urlGenerator->infoHTML($context);
93
                },
94
            ])
95
            ->add('id', TextColumn::class, [
96
                'label' => $this->translator->trans('part.table.id'),
97
                'visible' => false
98
            ])
99
            ->add('description', TextColumn::class, [
100
                'label' => $this->translator->trans('part.table.description'),
101
            ])
102
            ->add('category', TextColumn::class, [
103
                'field' => 'category.name',
104
                'label' => $this->translator->trans('part.table.category')
105
            ])
106
            //->add('footprint', TextColumn::class, ['field' => 'footprint.name'])
107
            //->add('manufacturer', TextColumn::class, ['field' => 'manufacturer.name' ])
108
            //->add('amountSum', TextColumn::class, ['label' => 'instock.label_short'])
109
            ->add('amount', TextColumn::class, [
110
                'label' => $this->translator->trans('part.table.amount'),
111
                'propertyPath' => 'amountSum'
112
            ])
113
            ->add('minamount', TextColumn::class, [
114
                'label' => $this->translator->trans('part.table.minamount')
115
            ])
116
            //->add('storelocation', TextColumn::class, ['field' => 'storelocation.name', 'label' => 'storelocation.label'])
117
118
            ->add('addedDate', DateTimeColumn::class, [
119
                'label' => $this->translator->trans('part.table.addedDate'),
120
                'visible' => false
121
            ])
122
            ->add('lastModified', DateTimeColumn::class, [
123
                'label' => $this->translator->trans('part.table.lastModified'),
124
                'visible' => false
125
            ])
126
            ->add('needs_review', BoolColumn::class, [
127
                'label' => $this->translator->trans('part.table.needsReview'),
128
                'trueValue' => $this->translator->trans('true'),
129
                'falseValue' => $this->translator->trans('false'),
130
                'nullValue' => '',
131
                'visible' => false
132
            ])
133
            ->add('favorite', BoolColumn::class, [
134
                'label' => $this->translator->trans('part.table.favorite'),
135
                'trueValue' => $this->translator->trans('true'),
136
                'falseValue' => $this->translator->trans('false'),
137
                'nullValue' => '',
138
                'visible' => false
139
            ])
140
            ->add('manufacturing_status', MapColumn::class, [
141
                'label' => $this->translator->trans('part.table.manufacturingStatus'),
142
                'visible' => false,
143
                'default' => $this->translator->trans('m_status.unknown'),
144
                'map' => [
145
                    '' => $this->translator->trans('m_status.unknown'),
146
                    'announced' => $this->translator->trans('m_status.announced'),
147
                    'active' => $this->translator->trans('m_status.active'),
148
                    'nrfnd' => $this->translator->trans('m_status.nrfnd'),
149
                    'eol' => $this->translator->trans('m_status.eol'),
150
                    'discontinued' => $this->translator->trans('m_status.discontinued')
151
                ]
152
            ])
153
            ->add('manufacturer_product_number', TextColumn::class, [
154
                'label' => $this->translator->trans('part.table.mpn'),
155
                'visible' => false
156
            ])
157
            ->add('mass', TextColumn::class, [
158
                'label' => $this->translator->trans('part.table.mass'),
159
                'visible' => false
160
            ])
161
            ->add('tags', TextColumn::class, [
162
                'label' => $this->translator->trans('part.table.tags'),
163
                'visible' => false
164
            ])
165
166
            ->addOrderBy('name')
167
            ->createAdapter(ORMAdapter::class, [
168
                'entity' => Part::class,
169
                'criteria' => [
170
                    function (QueryBuilder $builder) use ($options) {
171
                        $this->buildCriteria($builder, $options);
172
                    },
173
                    new SearchCriteriaProvider()
174
                ]
175
            ]);
176
    }
177
}
178