Completed
Pull Request — master (#148)
by
unknown
02:58
created

PerformanceEventAdmin::getSeat()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 45
Code Lines 34

Duplication

Lines 41
Ratio 91.11 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 34
nc 7
nop 4
dl 41
loc 45
ccs 19
cts 19
cp 1
crap 6
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace AppBundle\Admin;
4
5
use AppBundle\Entity\PriceCategory;
6
use Sonata\AdminBundle\Admin\Admin;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
9
use Sonata\AdminBundle\Exception\ModelManagerException;
10
use Sonata\AdminBundle\Form\FormMapper;
11
use Sonata\AdminBundle\Route\RouteCollection;
12
13
class PerformanceEventAdmin extends Admin
14
{
15
    protected $baseRouteName = 'AppBundle\Entity\PerformanceEvent';
16
    protected $baseRoutePattern = 'PerformanceEvent';
17
    protected $datagridValues = [
18
        '_sort_order' => 'DESC',
19
        '_sort_by'    => 'dateTime',
20
    ];
21
    protected $seatPrice = [];
22
23
    /**
24
     * @param RouteCollection $collection
25
     */
26 2
    protected function configureRoutes(RouteCollection $collection)
27
    {
28
        $collection
29 2
            ->add('getVenue')
30 2
            ->add('deletePriceCategories')
31
        ;
32 2
    }
33
34
35
    /**
36
     * @param FormMapper $formMapper
37
     *
38
     * @return void
39
     */
40 1
    protected function configureFormFields(FormMapper $formMapper)
41
    {
42
        $formMapper
43 1
            ->with('PerformanceEvents', ['class'=>'col-lg-12'])
44 1
            ->add('performance', 'sonata_type_model')
45 1
            ->add(
46 1
                'dateTime',
47 1
                'sonata_type_datetime_picker',
48
                [
49 1
                    'dp_side_by_side'       => true,
50
                    'dp_use_current'        => false,
51
                    'dp_use_seconds'        => false,
52
                    'format' => "dd/MM/yyyy HH:mm",
53
                ]
54
            )
55 1
            ->add('venue')
56 1
            ->end()
57 1
            ->with('PriceCategory', ['class'=>'col-lg-12'])
58 1
            ->add('priceCategories', 'sonata_type_collection', [
59 1
                'by_reference' => true,
60
                'required' => false,
61
                'cascade_validation' => true,
62
                'type_options'       => [
63
                    'delete' => true,
64
                ],
65
                'label' => false,
66
            ], [
67 1
                'inline'            => 'table',
68 1
                'edit'            => 'inline',
69 1
                'sortable'        => 'position',
70
                'link_parameters'       => [
71 1
                    'performanceEvent_id' => $this->getSubject()->getId(),
72
                ],
73
            ])
74 1
            ->end()
75 1
            ->with('EnableSale', ['class'=>'col-lg-12'])
76 1
            ->add(
77 1
                'seriesDate',
78 1
                'sonata_type_datetime_picker',
79
                [
80 1
                    'dp_side_by_side'       => true,
81
                    'dp_use_current'        => true,
82
                    'dp_use_seconds'        => false,
83
                    'format' => "dd/MM/yyyy HH:mm",
84
                    'required' => false,
85
                ]
86
            )
87 1
            ->add('seriesNumber', null, [
88 1
            'required' => false,
89
            ])
90 1
            ->add('enableSale', null, [
91 1
            'required' => false,
92
            ])
93 1
            ->end()
94
        ;
95 1
    }
96
97
    /**
98
     * @param ListMapper $listMapper
99
     *
100
     * @return void
101
     */
102 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...
103
    {
104
        $listMapper
105 2
            ->add('performance')
106 2
            ->add('dateTime')
107 2
            ->add('venue')
108 2
            ->add('_action', 'actions', [
109
                'actions' => [
110
                    'edit' => [],
111
                    'delete' => [],
112 2
                ],
113
            ])
114
        ;
115 2
    }
116
117
    /**
118
     * @param DatagridMapper $datagridMapper
119
     *
120
     * @return void
121
     */
122 2
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
123
    {
124
        $datagridMapper
125 2
            ->add('performance')
126 2
            ->add('venue')
127
        ;
128 2
    }
129
130 1
    public function postUpdate($object)
131
    {
132 1
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
133 1
        $categories = $em->getRepository('AppBundle:PriceCategory')->findBy(['performanceEvent' => $object]);
134 1
        $venue = $object->getVenue()->getTitle();
135
136
        /** @var PriceCategory $category*/
137
138 1
        foreach ($categories as $category) {
139 1
            self::getRows($venue, $category->getRows(), $category->getVenueSector(), $category->getPlaces());
140
        }
141
    }
142
143 1
    private function getSeat($venue, $row, $venueSector, $place = null)
144
    {
145 1
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
146 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...
147
            $seat = $em->getRepository('AppBundle:Seat')->findBy([
148
                'row' => $row,
149
                'venueSector' => $venueSector,
150
            ]);
151
            if (!$seat) {
152
                $this
153
                    ->getConfigurationPool()
154
                    ->getContainer()
155
                    ->get('session')
156
                    ->getFlashBag()
157
                    ->add(
158
                        'error',
159
                        "Помилка. В залi $venue немає $row ряда в секторі $venueSector!"
160
                    );
161
                throw new ModelManagerException('Error row!');
162
            }
163
            foreach ($seat as $placeAllInRow) {
164
                self::validateSeat($row, $placeAllInRow->getPlace(), $venueSector);
165
            }
166
        }
167 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...
168 1
            $seat = $em->getRepository('AppBundle:Seat')->findOneBy([
169 1
                'row' => $row,
170 1
                'place' => $place,
171 1
                'venueSector' => $venueSector,
172
            ]);
173 1
            if (!$seat) {
174
                $this
175 1
                    ->getConfigurationPool()
176 1
                    ->getContainer()
177 1
                    ->get('session')
178 1
                    ->getFlashBag()
179 1
                    ->add(
180 1
                        'error',
181 1
                        "Помилка. В залi $venue немає $row - $place в секторі $venueSector!"
182
                    );
183 1
                throw new ModelManagerException('Error row-place!');
184
            }
185 1
            self::validateSeat($row, $place, $venueSector);
186
        }
187 1
    }
188
189 1
    private function getPlaces($venue, $row, $venueSector, $strPlaces)
190
    {
191 1
        if ($strPlaces === null) {
192
            self::getSeat($venue, $row, $venueSector);
193
            return;
194
        }
195 1
        $dataPlaces = explode(',', $strPlaces);
196 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...
197 1
            if (substr_count($places, '-') === 1) {
198 1
                list($begin, $end) = explode('-', $places);
199 1
                for ($place = $begin; $place <= $end; $place++) {
200 1
                    self::getSeat($venue, $row, $venueSector, $place);
201
                }
202
            }
203 1
            if (substr_count($places, '-') === 0) {
204
                self::getSeat($venue, $row, $venueSector, $places);
205
            }
206
        }
207 1
    }
208
209 1
    private function getRows($venue, $strRows, $venueSector, $strPlaces)
210
    {
211 1
        $dataRows = explode(',', $strRows);
212 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...
213 1
            if (substr_count($rows, '-') === 1) {
214 1
                list($begin, $end) = explode('-', $rows);
215 1
                for ($row = $begin; $row <= $end; $row++) {
216 1
                    self::getPlaces($venue, $row, $venueSector, $strPlaces);
217
                }
218
            }
219
            if (substr_count($rows, '-') === 0) {
220
                self::getPlaces($venue, $rows, $venueSector, $strPlaces);
221
            }
222
        }
223
    }
224
225 1
    private function validateSeat($row, $place, $venueSector)
226
    {
227 1
        $seats = $this->seatPrice;
228 1
        foreach ($seats as $key) {
229 1
            if ($key === $row.'-'.$place) {
230
                $this
231
                    ->getConfigurationPool()
232
                    ->getContainer()
233
                    ->get('session')
234
                    ->getFlashBag()
235
                    ->add(
236
                        'error',
237
                        "Помилка. $row - $place в секторі $venueSector вже має цiну!"
238
                    );
239
                throw new ModelManagerException('Error row-place price!');
240
            }
241
        }
242 1
        $seats[]= $row.'-'.$place;
243 1
        $this->seatPrice = $seats;
244 1
    }
245
}
246