Completed
Pull Request — master (#154)
by
unknown
02:51
created

inspectSeatMoreThanOnePrice()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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