Completed
Push — master ( 5bb9e4...33b9a4 )
by Serhii
09:16 queued 09:08
created

GetSeatsHandler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 4 1
A getSeatsForPriceCategory() 0 20 4
A parsePriceCategoryPlaces() 0 10 2
B getPlacesFromString() 0 24 5
A removeEmptyElements() 0 6 1
A validatePlaces() 0 6 2
1
<?php
2
3
namespace AppBundle\Services\PriceCategory;
4
5
use AppBundle\Entity\PriceCategory;
6
use AppBundle\Entity\Seat;
7
use AppBundle\Exception\PriceCategory\PlaceArrangementException;
8
use AppBundle\Repository\SeatRepository;
9
10
class GetSeatsHandler
11
{
12
    /** @var SeatRepository */
13
    private $seatRepository;
14
15
    /**
16
     * @param SeatRepository $seatRepository
17
     */
18
    public function __construct(
19
        SeatRepository $seatRepository
20
    ) {
21
        $this->seatRepository = $seatRepository;
22
    }
23
24
    /**
25
     * @param PriceCategory $priceCategory
26
     *
27
     * @return Seat[]
28
     */
29
    public function handle(PriceCategory $priceCategory): array
30
    {
31
        return $this->getSeatsForPriceCategory($priceCategory);
32
    }
33
34
    /**
35
     * @param PriceCategory $priceCategory
36
     *
37
     * @return Seat[]
38
     */
39
    protected function getSeatsForPriceCategory(PriceCategory $priceCategory): array
40
    {
41
        $seats = [];
42
        $arrayPlaces = $this->parsePriceCategoryPlaces($priceCategory);
43
        foreach ($arrayPlaces as $row => $places) {
44
            if (empty($places)) {
45
                $seats = array_merge(
46
                    $seats,
47
                    $this->seatRepository->getByVenueSectorAndRow($priceCategory->getVenueSector(), $row)
48
                );
49
                continue;
50
            }
51
            foreach ($places as $place) {
52
                $seats[] = $this->seatRepository
53
                    ->getByVenueSectorRowAndPlace($priceCategory->getVenueSector(), $row, $place);
54
            }
55
        }
56
57
        return $seats;
58
    }
59
60
    /**
61
     * @param PriceCategory $priceCategory
62
     *
63
     * @return array
64
     */
65
    protected function parsePriceCategoryPlaces(PriceCategory $priceCategory): array
66
    {
67
        $rows = $this->getPlacesFromString($priceCategory->getRows());
68
        $places = [];
69
        foreach ($rows as $row) {
70
            $places[$row] = $this->getPlacesFromString($priceCategory->getPlaces());
71
        }
72
73
        return $places;
74
    }
75
76
    /**
77
     * @param string $incomingStrPlaces
78
     *
79
     * @return array
80
     */
81
    protected function getPlacesFromString(string $incomingStrPlaces = null): array
82
    {
83
        $places = [];
84
        $dataPlaces = $this->removeEmptyElements(explode(',', $incomingStrPlaces));
85
86
        foreach ($dataPlaces as $strPlaces) {
87
            if (substr_count($strPlaces, '-') === 0) {
88
                $places[] = (int) $strPlaces;
89
                continue;
90
            }
91
92
            if (substr_count($strPlaces, '-') === 1) {
93
                list($begin, $end) = explode('-', $strPlaces);
94
                $begin = (int) $begin;
95
                $end = (int) $end;
96
                for ($place = $begin; $place <= $end; $place++) {
97
                    $places[] = (int) $place;
98
                }
99
            }
100
        }
101
        $this->validatePlaces($places);
102
103
        return $places;
104
    }
105
106
    /**
107
     * @param array $array
108
     *
109
     * @return array
110
     */
111
    protected function removeEmptyElements(array $array): array
112
    {
113
        return array_filter($array, function ($value) {
114
            return !empty($value);
115
        });
116
    }
117
118
    /**
119
     * @param array $places
120
     * @throws \Exception
121
     */
122
    protected function validatePlaces(array $places)
123
    {
124
        if (count(array_unique($places)) < count($places)) {
125
            throw new PlaceArrangementException('Places arranged incorrectly. Duplicates appears.');
126
        }
127
    }
128
}
129