SuperimmoUrlBuilder::buildQueryParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 7
dl 0
loc 15
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\UrlBuilder;
4
5
use App\DTO\City;
6
use App\Enum\PropertyType;
7
8
class SuperimmoUrlBuilder extends AbstractUrlBuilder
9
{
10
    private const MAX_ROOMS_COUNT = 5;
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 sprintf('https://www.superimmo.com/achat/%s%s/%s',
26
            $this->buildPropertyTypesParam($propertyTypes),
27
            $this->buildLocationParam($city),
28
            $this->buildRoomsCountParam($minRoomsCount)
29
        );
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    protected function buildQueryParameters(
36
        City $city,
37
        array $propertyTypes,
38
        ?int $minPrice,
39
        int $maxPrice,
40
        ?int $minArea,
41
        ?int $maxArea,
42
        int $minRoomsCount
43
    ): array
44
    {
45
        return [
46
            $this->buildAreaParam('max', $maxArea),
47
            $this->buildAreaParam('min', $minArea),
48
            $this->buildPriceParam('max', $maxPrice),
49
            $this->buildPriceParam('min', $minPrice)
50
        ];
51
    }
52
53
    /**
54
     * @param City $city
55
     *
56
     * @return string
57
     */
58
    private function buildLocationParam(City $city): string
59
    {
60
        return sprintf('%s/%s/%s-%s', $city->getRegion(), $city->getDepartment(), $city->getName(), $city->getDepartmentCode());
61
    }
62
63
    /**
64
     * @param string[] $types
65
     *
66
     * @return string
67
     */
68
    private function buildPropertyTypesParam(array $types): string
69
    {
70
        if (2 === count($types)) {
71
            return '';
72
        }
73
74
        return str_replace([PropertyType::APARTMENT, PropertyType::HOUSE], ['appartement/', 'maison/'], $types[0]);
75
    }
76
77
    /**
78
     * @param string   $bound
79
     * @param int|null $price
80
     *
81
     * @return array
82
     */
83
    private function buildPriceParam(string $bound, ?int $price): array
84
    {
85
        $priceStr = null !== $price ? sprintf('%d.0', $price) : null;
86
87
        return ["price_$bound" => $priceStr];
88
    }
89
90
    /**
91
     * @param string   $bound
92
     * @param int|null $area
93
     *
94
     * @return array
95
     */
96
    private function buildAreaParam(string $bound, ?int $area): array
97
    {
98
        $areaStr = null !== $area ? sprintf('%d.0', $area) : null;
99
100
        return ["area_$bound" => $areaStr];
101
    }
102
103
    /**
104
     * @param int $minRoomsCount
105
     *
106
     * @return string
107
     */
108
    private function buildRoomsCountParam(int $minRoomsCount): string
109
    {
110
        return sprintf('pieces-%s', min($minRoomsCount, self::MAX_ROOMS_COUNT));
111
    }
112
}
113