Completed
Pull Request — master (#136)
by
unknown
19:52 queued 08:32
created

PerformanceEventAdmin::getPlaces()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 11
Ratio 57.89 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 4
dl 11
loc 19
ccs 10
cts 10
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Admin;
4
5
use AppBundle\Entity\PerformanceEvent;
6
use AppBundle\Entity\PriceCategory;
7
use AppBundle\Entity\RowsForSale;
8
use AppBundle\Entity\Seat;
9
use AppBundle\Entity\Venue;
10
use AppBundle\Entity\VenueSector;
11
use Sonata\AdminBundle\Admin\Admin;
12
use Sonata\AdminBundle\Datagrid\ListMapper;
13
use Sonata\AdminBundle\Exception\ModelManagerException;
14
use Sonata\AdminBundle\Form\FormMapper;
15
use Sonata\AdminBundle\Route\RouteCollection;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
18
class PerformanceEventAdmin extends Admin
19
{
20
    protected $baseRouteName = 'AppBundle\Entity\PerformanceEvent';
21
    protected $baseRoutePattern = 'PerformanceEvent';
22
    protected $datagridValues = [
23
        '_sort_order' => 'DESC',
24
        '_sort_by'    => 'dateTime',
25
    ];
26
    protected $seatPrice = [];
27
28
    /**
29
     * @param RouteCollection $collection
30
     */
31 2
    protected function configureRoutes(RouteCollection $collection)
32
    {
33
        $collection
34 2
            ->add('getVenue')
35 2
            ->add('deletePriceCategories')
36
        ;
37 2
    }
38
39
    /**
40
     * @param FormMapper $formMapper
41
     *
42
     * @return void
43
     */
44 1
    protected function configureFormFields(FormMapper $formMapper)
45
    {
46 1
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.default_entity_manager');
47 1
        $queryRowsForSale = $em->getRepository('AppBundle:RowsForSale')
48 1
            ->findVenueSectorsByPerformanceEventQueryBuilder($this->getSubject());
49
50
        $formMapper
51 1
            ->with('PerformanceEvents', ['class'=>'col-lg-12'])
52 1
            ->add('performance', 'sonata_type_model')
53 1
            ->add(
54 1
                'dateTime',
55 1
                'sonata_type_datetime_picker',
56
                [
57 1
                    'dp_side_by_side'       => true,
58
                    'dp_use_current'        => false,
59
                    'dp_use_seconds'        => false,
60
                    'format' => "dd/MM/yyyy HH:mm",
61
                ]
62
            )
63 1
            ->add('venue')
64 1
            ->end()
65 1
            ->with('PriceCategory', ['class'=>'col-lg-12'])
66 1
            ->add('priceCategories', 'sonata_type_collection', [
67 1
                'by_reference' => true,
68
                'required' => false,
69
                'cascade_validation' => true,
70
                'type_options'       => [
71
                    'delete' => true,
72
                ],
73
                'label' => false,
74
            ], [
75 1
                'inline'  => 'table',
76 1
                'edit' => 'inline',
77 1
                'sortable' => 'position',
78
                'link_parameters'       => [
79 1
                    'performanceEvent_id' => $this->getSubject()->getId(),
80
                ],
81
            ])
82 1
            ->end()
83 1
            ->with('EnableSale', ['class'=>'col-lg-12'])
84 1
            ->add('seriesDate', 'sonata_type_datetime_picker', [
85 1
                'dp_side_by_side'       => true,
86
                'dp_use_current'        => true,
87
                'dp_use_seconds'        => false,
88
                'format' => "dd/MM/yyyy HH:mm",
89
                'required' => false,
90
            ])
91 1
            ->add('rowsForSale', 'sonata_type_model', [
92 1
                'class' => RowsForSale::class,
93
                'required' => false,
94
                'multiple' => true,
95 1
                'query' => $queryRowsForSale,
96
            ])
97
        ;
98 1
        if ($this->getSubject()->isEnableSale() !== true) {
99
            $formMapper
100 1
                ->add('seriesNumber', null, [
101 1
                    'required' => false,
102
                ])
103 1
                ->add('enableSale', 'checkbox', [
104 1
                    'required' => false,
105
                ])
106 1
                ->end()
107
            ;
108
        }
109 1
        if ($this->getSubject()->isEnableSale() === true) {
110
            $formMapper
111
                ->add('seriesNumber', null, [
112
                    'required' => false,
113
                    'attr' => ['class' => 'hidden'],
114
                    'label' => false,
115
                ])
116
                ->add('enableSale', TextType::class, [
117
                    'required' => false,
118
                    'attr' => ['class' => 'hidden'],
119
                    'label' => false,
120
                ])
121
                ->end()
122
            ;
123
        }
124 1
    }
125
126
    /**
127
     * @param ListMapper $listMapper
128
     *
129
     * @return void
130
     */
131 2 View Code Duplication
    protected function configureListFields(ListMapper $listMapper)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
132
    {
133
        $listMapper
134 2
            ->add('performance')
135 2
            ->add('dateTime')
136 2
            ->add('venue')
137 2
            ->add('_action', 'actions', [
138
                'actions' => [
139
                    'edit' => [],
140
                    'delete' => [],
141 2
                ],
142
            ]);
143 2
    }
144
145 1
    public function postUpdate($object)
146
    {
147 1
        self::inspectPriceCategories($object);
148
    }
149
150
    /**
151
     * Inspect PriceCategory. Search errors
152
     *
153
     * @param PerformanceEvent $performanceEvent
154
     * @throws ModelManagerException
155
     */
156 1
    private function inspectPriceCategories(PerformanceEvent $performanceEvent)
157
    {
158 1
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
159 1
        $categories = $em->getRepository('AppBundle:PriceCategory')->findBy(['performanceEvent' => $performanceEvent]);
160 1
        $seat = $em->getRepository('AppBundle:Seat')->findByVenue($performanceEvent->getVenue());
161 1
        $venue = $performanceEvent->getVenue();
162
163
        /** @var PriceCategory $category*/
164
165 1
        foreach ($categories as $category) {
166 1
            self::getRows($venue, $category->getRows(), $category->getVenueSector(), $category->getPlaces());
167
        }
168
        self::inspectSeatWithoutPrice(count($seat), $performanceEvent, $venue);
169 View Code Duplication
        if (!$performanceEvent->getSeriesNumber()) {
0 ignored issues
show
Duplication introduced by
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...
170
            $performanceEvent->setEnableSale(null);
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
171
            $em->persist($performanceEvent);
172
            $em->flush();
173
            $this
174
                ->getConfigurationPool()
175
                ->getContainer()
176
                ->get('session')
177
                ->getFlashBag()
178
                ->add(
179
                    'error',
180
                    "Помилка. Введіть номер комплекта квитків!"
181
                );
182
            throw new ModelManagerException();
183
        }
184
        if ((count($seat) === count($this->seatPrice)) && ($performanceEvent->isEnableSale() === null)) {
185
            $performanceEvent->setEnableSale(false);
186
            $em->persist($performanceEvent);
187
            $em->flush();
188
        }
189
    }
190
191
    /**
192
     * Parse string rows in PriceCategory
193
     *
194
     * @param Venue $venue
195
     * @param $strRows
196
     * @param VenueSector $venueSector
197
     * @param $strPlaces
198
     */
199 1
    private function getRows(Venue $venue, $strRows, VenueSector $venueSector, $strPlaces)
200
    {
201 1
        $dataRows = explode(',', $strRows);
202 1 View Code Duplication
        foreach ($dataRows as $rows) {
0 ignored issues
show
Duplication introduced by
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...
203 1
            if (substr_count($rows, '-') === 1) {
204 1
                list($begin, $end) = explode('-', $rows);
205 1
                for ($row = $begin; $row <= $end; $row++) {
206 1
                    self::getPlaces($venue, $row, $venueSector, $strPlaces);
207
                }
208
            }
209
            if (substr_count($rows, '-') === 0) {
210
                self::getPlaces($venue, $rows, $venueSector, $strPlaces);
211
            }
212
        }
213
    }
214
215
    /**
216
     * Parse string places in PriceCategory
217
     *
218
     * @param Venue $venue
219
     * @param $row
220
     * @param VenueSector $venueSector
221
     * @param $strPlaces
222
     */
223 1
    private function getPlaces(Venue $venue, $row, VenueSector $venueSector, $strPlaces)
224
    {
225 1
        if ($strPlaces === null) {
226
            self::getSeat($venue, $row, $venueSector);
227
            return;
228
        }
229 1
        $dataPlaces = explode(',', $strPlaces);
230 1 View Code Duplication
        foreach ($dataPlaces as $places) {
0 ignored issues
show
Duplication introduced by
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...
231 1
            if (substr_count($places, '-') === 1) {
232 1
                list($begin, $end) = explode('-', $places);
233 1
                for ($place = $begin; $place <= $end; $place++) {
234 1
                    self::getSeat($venue, $row, $venueSector, $place);
235
                }
236
            }
237 1
            if (substr_count($places, '-') === 0) {
238
                self::getSeat($venue, $row, $venueSector, $places);
239
            }
240
        }
241 1
    }
242
243
    /**
244
     * Research existing Seat with row-place - $row-$place
245
     *
246
     * @param Venue $venue
247
     * @param $row
248
     * @param VenueSector $venueSector
249
     * @param null $place
250
     * @throws ModelManagerException
251
     */
252 1
    private function getSeat(Venue $venue, $row, VenueSector $venueSector, $place = null)
253
    {
254 1
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
255 1 View Code Duplication
        if ($place === null) {
0 ignored issues
show
Duplication introduced by
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...
256
            $seat = $em->getRepository('AppBundle:Seat')->findBy([
257
                'row' => $row,
258
                'venueSector' => $venueSector,
259
            ]);
260
            if (!$seat) {
261
                $this
262
                    ->getConfigurationPool()
263
                    ->getContainer()
264
                    ->get('session')
265
                    ->getFlashBag()
266
                    ->add(
267
                        'error',
268
                        "Помилка. В залi $venue немає $row ряда в секторі $venueSector!"
269
                    );
270
                throw new ModelManagerException('Error row!');
271
            }
272
            foreach ($seat as $placeAllInRow) {
273
                self::inspectSeatMoreThanOnePrice($row, $placeAllInRow->getPlace(), $venueSector);
274
            }
275
        }
276 1 View Code Duplication
        if ($place !== null) {
0 ignored issues
show
Duplication introduced by
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...
277 1
            $seat = $em->getRepository('AppBundle:Seat')->findOneBy([
278 1
                'row' => $row,
279 1
                'place' => $place,
280 1
                'venueSector' => $venueSector,
281
            ]);
282 1
            if (!$seat) {
283
                $this
284 1
                    ->getConfigurationPool()
285 1
                    ->getContainer()
286 1
                    ->get('session')
287 1
                    ->getFlashBag()
288 1
                    ->add(
289 1
                        'error',
290 1
                        "Помилка. В залi $venue немає $row - $place в секторі $venueSector!"
291
                    );
292 1
                throw new ModelManagerException('Error row-place!');
293
            }
294 1
            self::inspectSeatMoreThanOnePrice($row, $place, $venueSector);
295
        }
296 1
    }
297
298
    /**
299
     * Search Seat with more than one price
300
     *
301
     * @param $row
302
     * @param $place
303
     * @param VenueSector $venueSector
304
     * @throws ModelManagerException
305
     */
306 1
    private function inspectSeatMoreThanOnePrice($row, $place, VenueSector $venueSector)
307
    {
308 1
        $seats = $this->seatPrice;
309 1
        foreach ($seats as $key) {
310 1
            if ($key === $row.'-'.$place) {
311
                $this
312
                    ->getConfigurationPool()
313
                    ->getContainer()
314
                    ->get('session')
315
                    ->getFlashBag()
316
                    ->add(
317
                        'error',
318
                        "Помилка. $row - $place в секторі $venueSector вже має цiну!"
319
                    );
320
                throw new ModelManagerException('Error row-place price!');
321
            }
322
        }
323 1
        $seats[]= $row.'-'.$place;
324 1
        $this->seatPrice = $seats;
325 1
    }
326
327
    /**
328
     * Search Seat without price
329
     *
330
     * @param $countSeat
331
     * @param PerformanceEvent $performanceEvent
332
     * @param Venue $venue
333
     * @throws ModelManagerException
334
     */
335
    private function inspectSeatWithoutPrice($countSeat, PerformanceEvent $performanceEvent, Venue $venue)
336
    {
337
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
338 View Code Duplication
        if ($countSeat != count($this->seatPrice)) {
0 ignored issues
show
Duplication introduced by
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...
339
            $performanceEvent->setEnableSale(null);
0 ignored issues
show
Documentation introduced by
null is of type null, 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...
340
            $em->persist($performanceEvent);
341
            $em->flush();
342
            $this
343
                ->getConfigurationPool()
344
                ->getContainer()
345
                ->get('session')
346
                ->getFlashBag()
347
                ->add(
348
                    'error',
349
                    "Помилка. В залi $venue ціна проставлена не на всі місця!"
350
                );
351
            throw new ModelManagerException();
352
        }
353
    }
354
}
355