LeBonCoinUrlBuilder::buildQueryParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 24
ccs 0
cts 10
cp 0
rs 9.9332
cc 2
nc 2
nop 7
crap 6
1
<?php
2
3
namespace App\UrlBuilder;
4
5
use App\DTO\City;
6
use App\Enum\PropertyType;
7
8
class LeBonCoinUrlBuilder extends AbstractUrlBuilder
9
{
10
    private const MAX_ROOMS_COUNT = 8;
11
12
    /**
13
     * {@inheritDoc}
14
     */
15
    protected function buildPath(
16
        City $city,
17
        array $propertyTypes,
18
        ?int $minPrice,
19
        int $maxPrice,
20
        ?int $minArea,
21
        ?int $maxArea,
22
        int $minRoomsCount
23
    ): string
24
    {
25
        return 'https://www.leboncoin.fr/recherche/';
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function buildQueryParameters(
32
        City $city,
33
        array $propertyTypes,
34
        ?int $minPrice,
35
        int $maxPrice,
36
        ?int $minArea,
37
        ?int $maxArea,
38
        int $minRoomsCount
39
    ): array
40
    {
41
        $params = [
42
            ['category' => 9],
43
            $this->buildLocationParam($city),
44
            $this->buildPropertyTypesParam($propertyTypes),
45
            $this->buildPriceParam($minPrice, $maxPrice),
46
            $this->buildAreaParam($minArea, $maxArea),
47
            ['immo_sell_type' => 'old,new'],
48
        ];
49
50
        if ($minRoomsCount > 1) {
51
            $params[] = $this->buildRoomsCountParam($minRoomsCount);
52
        }
53
54
        return $params;
55
    }
56
57
    /**
58
     * @param City $city
59
     *
60
     * @return array
61
     */
62
    private function buildLocationParam(City $city): array
63
    {
64
        return ['locations' => ucfirst($city->getName())];
65
    }
66
67
    /**
68
     * @param string[] $types
69
     *
70
     * @return array
71
     */
72
    private function buildPropertyTypesParam(array $types): array
73
    {
74
        $types = str_replace([PropertyType::HOUSE, PropertyType::APARTMENT], [1, 2], $types);
75
76
        return ['real_estate_type' => implode(',', $types)];
77
    }
78
79
    /**
80
     * @param int|null $minPrice
81
     * @param int      $maxPrice
82
     *
83
     * @return array
84
     */
85
    private function buildPriceParam(?int $minPrice, int $maxPrice): array
86
    {
87
        $minPrice = $minPrice ?: 'min';
88
89
        return ['price' => "$minPrice-$maxPrice"];
90
    }
91
92
    /**
93
     * @param int|null $minArea
94
     * @param int|null $maxArea
95
     *
96
     * @return array
97
     */
98
    private function buildAreaParam(?int $minArea, ?int $maxArea = null): array
99
    {
100
        if (null === $minArea && null === $maxArea) {
101
            return [];
102
        }
103
104
        $minArea = $minArea ?: 'min';
105
        $maxArea = $maxArea ?: 'max';
106
107
        return ['square' => "$minArea-$maxArea"];
108
    }
109
110
    /**
111
     * @param int $minRoomsCount
112
     *
113
     * @return array
114
     */
115
    private function buildRoomsCountParam(int $minRoomsCount): array
116
    {
117
        $minRoomsCount = min($minRoomsCount, self::MAX_ROOMS_COUNT);
118
119
        return ['rooms' => "$minRoomsCount-max"];
120
    }
121
}
122