OuestFranceImmoUrlBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 102
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPriceParam() 0 5 2
A buildAreaParam() 0 10 5
A buildRoomsCountParam() 0 3 1
A buildPropertyTypesParam() 0 5 1
A buildLocationParam() 0 3 1
A buildPath() 0 11 1
A buildQueryParameters() 0 15 1
1
<?php
2
3
namespace App\UrlBuilder;
4
5
use App\DTO\City;
6
use App\Enum\PropertyType;
7
8
class OuestFranceImmoUrlBuilder extends AbstractUrlBuilder
9
{
10
    private const MAX_ROOMS_COUNT = 6;
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.ouestfrance-immo.com/acheter/%s/', $this->buildLocationParam($city));
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
        return [
42
            $this->buildPropertyTypesParam($propertyTypes),
43
            $this->buildPriceParam($minPrice, $maxPrice),
44
            $this->buildAreaParam($minArea, $maxArea),
45
            $this->buildRoomsCountParam($minRoomsCount)
46
        ];
47
    }
48
49
    /**
50
     * @param City $city
51
     *
52
     * @return string
53
     */
54
    private function buildLocationParam(City $city): string
55
    {
56
        return sprintf('%s-%s-%s', $city->getName(), $city->getDepartmentCode(), $city->getZipCode());
57
    }
58
59
    /**
60
     * @param string[] $types
61
     *
62
     * @return array
63
     */
64
    private function buildPropertyTypesParam(array $types): array
65
    {
66
        $types = str_replace([PropertyType::APARTMENT, PropertyType::HOUSE], ['appartement', 'maison'], $types);
67
68
        return ['types' => implode(',', $types)];
69
    }
70
71
    /**
72
     * @param int|null $minPrice
73
     * @param int|null $maxPrice
74
     *
75
     * @return array
76
     */
77
    private function buildPriceParam(?int $minPrice, int $maxPrice): array
78
    {
79
        $minPrice = $minPrice ?: 0;
80
81
        return ['prix' => sprintf('%d_%d', $minPrice, $maxPrice)];
82
    }
83
84
    /**
85
     * @param int|null $minArea
86
     * @param int|null $maxArea
87
     *
88
     * @return array
89
     */
90
    private function buildAreaParam(?int $minArea, ?int $maxArea): array
91
    {
92
        if (null === $minArea && null === $maxArea) {
93
            return [];
94
        }
95
96
        $minArea = $minArea ?: 0;
97
        $maxArea = $maxArea ?: 0;
98
99
        return ['surface' => sprintf('%d_%d', $minArea, $maxArea)];
100
    }
101
102
    /**
103
     * @param int $minRoomsCount
104
     *
105
     * @return array
106
     */
107
    private function buildRoomsCountParam(int $minRoomsCount): array
108
    {
109
        return ['pieces' => sprintf('%d_0', min($minRoomsCount, self::MAX_ROOMS_COUNT))];
110
    }
111
}
112