Completed
Pull Request — master (#151)
by Ihor
13:44
created

GetSeatsHandler::getPlacesFromString()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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