OuestFranceImmoNeufUrlBuilder::buildPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 7
dl 0
loc 13
ccs 0
cts 4
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 OuestFranceImmoNeufUrlBuilder 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.ouestfrance-immo.com/immobilier-neuf/%sprogramme-neuf/%s/',
26
            $this->buildPropertyTypesParam($propertyTypes),
27
            $this->buildLocationParam($city)
28
        );
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    protected function buildQueryParameters(
35
        City $city,
36
        array $propertyTypes,
37
        ?int $minPrice,
38
        int $maxPrice,
39
        ?int $minArea,
40
        ?int $maxArea,
41
        int $minRoomsCount
42
    ): array
43
    {
44
        return [
45
            $this->buildRoomsCountParam($minRoomsCount),
46
            $this->buildPriceParam('max', $maxPrice),
47
            $this->buildAreaParam('min', $minArea),
48
            $this->buildAreaParam('max', $maxArea)
49
        ];
50
    }
51
52
    /**
53
     * @param City $city
54
     *
55
     * @return string
56
     */
57
    private function buildLocationParam(City $city): string
58
    {
59
        return sprintf('%s-%s', $city->getName(), $city->getZipCode());
60
    }
61
62
    /**
63
     * @param string[] $types
64
     *
65
     * @return string
66
     */
67
    private function buildPropertyTypesParam(array $types): string
68
    {
69
        if (2 === count($types)) {
70
            return '';
71
        }
72
73
        return str_replace([PropertyType::APARTMENT, PropertyType::HOUSE], ['appartement-neuf/', 'maison-neuve/'], $types[0]);
74
    }
75
76
    /**
77
     * @param string $bound
78
     * @param int    $price
79
     *
80
     * @return array
81
     */
82
    private function buildPriceParam(string $bound, int $price): array
83
    {
84
        return ["prix_$bound" => $price];
85
    }
86
87
    /**
88
     * @param string   $bound
89
     * @param int|null $area
90
     *
91
     * @return array
92
     */
93
    private function buildAreaParam(string $bound, ?int $area): array
94
    {
95
        return ["surface_$bound" => $area];
96
    }
97
98
    /**
99
     * @param int $minRoomsCount
100
     *
101
     * @return array
102
     */
103
    private function buildRoomsCountParam(int $minRoomsCount): array
104
    {
105
        return ['pieces' => sprintf('%d', min($minRoomsCount, self::MAX_ROOMS_COUNT))];
106
    }
107
}
108