PapUrlBuilder::buildAreaParam()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 30
rs 9.6111
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 PapUrlBuilder 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.pap.fr/annonce/vente-%s-%s-%s-%s-%s',
26
            $this->buildPropertyTypeParam($propertyTypes),
27
            $this->buildLocationParam($city),
28
            $this->buildRoomsCountParam($minRoomsCount),
29
            $this->buildPriceParam($minPrice, $maxPrice),
30
            $this->buildAreaParam($minArea, $maxArea),
31
        );
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    protected function buildQueryParameters(
38
        City $city,
39
        array $propertyTypes,
40
        ?int $minPrice,
41
        int $maxPrice,
42
        ?int $minArea,
43
        ?int $maxArea,
44
        int $minRoomsCount
45
    ): array
46
    {
47
        return [];
48
    }
49
50
    /**
51
     * @param City $city
52
     *
53
     * @return string
54
     */
55
    private function buildLocationParam(City $city): string
56
    {
57
        return sprintf('%s-%s-g%d', $city->getName(), $city->getDepartmentCode(), $city->getPapCode());
58
    }
59
60
    /**
61
     * @param string[] $types
62
     *
63
     * @return string
64
     */
65
    private function buildPropertyTypeParam(array $types): string
66
    {
67
        if (2 === count($types)) {
68
            $types = str_replace([PropertyType::APARTMENT, PropertyType::HOUSE], ['appartement', 'maison'], $types);
69
70
            return implode('-', $types);
71
        }
72
73
        return str_replace([PropertyType::APARTMENT, PropertyType::HOUSE], ['appartements', 'maisons'], $types[0]);
74
    }
75
76
    /**
77
     * @param int|null $minPrice
78
     * @param int      $maxPrice
79
     *
80
     * @return string
81
     */
82
    private function buildPriceParam(?int $minPrice, int $maxPrice): string
83
    {
84
        if (null === $minPrice) {
85
            return "jusqu-a-$maxPrice-euros";
86
        }
87
88
        return "entre-$minPrice-et-$maxPrice-euros";
89
    }
90
91
    /**
92
     * @param int|null $minArea
93
     * @param int|null $maxArea
94
     *
95
     * @return string
96
     */
97
    private function buildAreaParam(?int $minArea, ?int $maxArea): string
98
    {
99
        if (null !== $minArea && null !== $maxArea) {
100
            return "entre-$minArea-et-$maxArea-m2";
101
        }
102
103
        if (null !== $minArea) {
104
            return "a-partir-de-$minArea-m2";
105
        }
106
107
        if (null !== $maxArea) {
108
            return "jusqu-a-$maxArea-m2";
109
        }
110
111
        return '';
112
    }
113
114
    /**
115
     * @param int $minRoomsCount
116
     *
117
     * @return string
118
     */
119
    private function buildRoomsCountParam(int $minRoomsCount): string
120
    {
121
        if (1 === $minRoomsCount) {
122
            return 'studio';
123
        }
124
125
        $minRoomsCount = min($minRoomsCount, self::MAX_ROOMS_COUNT);
126
        $minRoomsCountStr = "$minRoomsCount-pieces";
127
128
        if (self::MAX_ROOMS_COUNT === $minRoomsCount) {
129
            $minRoomsCountStr = "a-partir-du-$minRoomsCountStr";
130
        }
131
132
        return $minRoomsCountStr;
133
    }
134
}
135