Completed
Pull Request — master (#136)
by Ihor
11:37
created

PerformanceEventAdmin   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 233
Duplicated Lines 33.05 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 77
loc 233
ccs 19
cts 19
cp 1
rs 10
c 2
b 0
f 0
wmc 26
lcom 1
cbo 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A configureRoutes() 0 7 1
A configureListFields() 14 14 1
A configureDatagridFilters() 0 7 1
A postUpdate() 0 12 2
B getSeat() 41 45 6
B getPlaces() 11 19 6
B getRows() 11 15 5
A validateSeat() 0 20 3
A configureFormFields() 0 56 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 1
     * @param RouteCollection $collection
25
     */
26
    protected function configureRoutes(RouteCollection $collection)
27 1
    {
28 1
        $collection
29 1
            ->add('getVenue')
30 1
            ->add('deletePriceCategories')
31
        ;
32 1
    }
33
34
35
    /**
36
     * @param FormMapper $formMapper
37
     *
38 1
     * @return void
39
     */
40 1
    protected function configureFormFields(FormMapper $formMapper)
41
    {
42
        $formMapper
43
            ->with('PerformanceEvents', ['class'=>'col-lg-12'])
44
            ->add('performance', 'sonata_type_model')
45
            ->add(
46
                'dateTime',
47 2
                'sonata_type_datetime_picker',
48
                [
49
                    'dp_side_by_side'       => true,
50 2
                    'dp_use_current'        => false,
51 2
                    'dp_use_seconds'        => false,
52 2
                    'format' => "dd/MM/yyyy HH:mm",
53 2
                ]
54
            )
55
            ->add('venue')
56
            ->end()
57 2
            ->with('PriceCategory', ['class'=>'col-lg-12'])
58
            ->add('priceCategories', 'sonata_type_collection', [
59
                'by_reference' => true,
60 2
                'required' => false,
61
                'cascade_validation' => true,
62
                'type_options'       => [
63
                    'delete' => true,
64
                ],
65
                'label' => false,
66
            ], [
67 2
                'inline'            => 'table',
68
                'edit'            => 'inline',
69
                'sortable'        => 'position',
70 2
                'link_parameters'       => [
71 2
                    'performanceEvent_id' => $this->getSubject()->getId(),
72
                ],
73 2
            ])
74
            ->end()
75
            ->with('EnableSale', ['class'=>'col-lg-12'])
76
            ->add(
77
                'seriesDate',
78
                'sonata_type_datetime_picker',
79
                [
80
                    '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
            ->add('seriesNumber', null, [
88
            'required' => false,
89
            ])
90
            ->add('enableSale', null, [
91
            'required' => false,
92
            ])
93
            ->end()
94
        ;
95
    }
96
97
    /**
98
     * @param ListMapper $listMapper
99
     *
100
     * @return void
101
     */
102 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
            ->add('performance')
106
            ->add('dateTime')
107
            ->add('venue')
108
            ->add('_action', 'actions', [
109
                'actions' => [
110
                    'edit' => [],
111
                    'delete' => [],
112
                ],
113
            ])
114
        ;
115
    }
116
117
    /**
118
     * @param DatagridMapper $datagridMapper
119
     *
120
     * @return void
121
     */
122
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
123
    {
124
        $datagridMapper
125
            ->add('performance')
126
            ->add('venue')
127
        ;
128
    }
129
130
    public function postUpdate($object)
131
    {
132
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
133
        $categories = $em->getRepository('AppBundle:PriceCategory')->findBy(['performanceEvent' => $object]);
134
        $venue = $object->getVenue()->getTitle();
135
136
        /** @var PriceCategory $category*/
137
138
        foreach ($categories as $category) {
139
            self::getRows($venue, $category->getRows(), $category->getVenueSector(), $category->getPlaces());
140
        }
141
    }
142
143
    private function getSeat($venue, $row, $venueSector, $place = null)
144
    {
145
        $em = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getManager();
146 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 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
            $seat = $em->getRepository('AppBundle:Seat')->findOneBy([
169
                'row' => $row,
170
                'place' => $place,
171
                'venueSector' => $venueSector,
172
            ]);
173
            if (!$seat) {
174
                $this
175
                    ->getConfigurationPool()
176
                    ->getContainer()
177
                    ->get('session')
178
                    ->getFlashBag()
179
                    ->add(
180
                        'error',
181
                        "Помилка. В залi $venue немає $row - $place в секторі $venueSector!"
182
                    );
183
                throw new ModelManagerException('Error row-place!');
184
            }
185
            self::validateSeat($row, $place, $venueSector);
186
        }
187
    }
188
189
    private function getPlaces($venue, $row, $venueSector, $strPlaces)
190
    {
191
        if ($strPlaces === null) {
192
            self::getSeat($venue, $row, $venueSector);
193
            return;
194
        }
195
        $dataPlaces = explode(',', $strPlaces);
196 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
            if (substr_count($places, '-') === 1) {
198
                list($begin, $end) = explode('-', $places);
199
                for ($place = $begin; $place <= $end; $place++) {
200
                    self::getSeat($venue, $row, $venueSector, $place);
201
                }
202
            }
203
            if (substr_count($places, '-') === 0) {
204
                self::getSeat($venue, $row, $venueSector, $places);
205
            }
206
        }
207
    }
208
209
    private function getRows($venue, $strRows, $venueSector, $strPlaces)
210
    {
211
        $dataRows = explode(',', $strRows);
212 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
            if (substr_count($rows, '-') === 1) {
214
                list($begin, $end) = explode('-', $rows);
215
                for ($row = $begin; $row <= $end; $row++) {
216
                    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
    private function validateSeat($row, $place, $venueSector)
226
    {
227
        $seats = $this->seatPrice;
228
        foreach ($seats as $key) {
229
            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
        $seats[]= $row.'-'.$place;
243
        $this->seatPrice = $seats;
244
    }
245
}
246