-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculateScoreController.php
234 lines (198 loc) · 6.41 KB
/
CalculateScoreController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
declare(strict_types=1);
namespace App\Infrastructure\Api;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Infrastructure\Persistence\InFileSystemPersistence;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use DateTimeImmutable;
final class CalculateScoreController
{
private InFileSystemPersistence $persistence;
private Helpers $helpers;
/**
* Constructor accepting an InFileSystemPersistence object
*
* @param InFileSystemPersistence $persistence
*/
public function __construct(InFileSystemPersistence $persistence, Helpers $helper)
{
$this->persistence = $persistence;
$this->helpers = $helper;
}
/**
* Invokable method returning a JsonResponse
*
* @return JsonResponse
*/
public function __invoke(): JsonResponse
{
$ads = $this->onCache();
return new JsonResponse($ads);
}
/**
* Caching method that stores ads data in a cache and returns the data
*
* @return void
*/
public function onCache()
{
$cache = new FilesystemAdapter();
// TODO: Check cache should update or not
$cacheItem = $cache->getItem('ads');
$cache->deleteItem('ads');
$data = $this->getAdsAndScore();
$cacheItem->set($data);
$cache->save($cacheItem);
return $data;
}
/**
* Retrieves ads data and their scores
*
* @return array
*/
public function getAdsAndScore(): array
{
$ads = $this->persistence->getAds();
$scores = [];
foreach ($ads as $ad) {
$score = $this->calculateScore($ad);
$ad->setScore($score);
$date = $score < 40 ? $this->helpers->calculateTime() : null;
if( $date instanceof DateTimeImmutable )
{
$ad->setIrrelevantSince($date);
$date = $date->format('Y-m-d');
}
$scores[] = [
'id' => $ad->getId(),
'typology' => $ad->getTypology(),
'description' => $ad->getDescription(),
'pictures' => $ad->getPictures(),
'houseSize' => $ad->getHouseSize(),
'gardenSize' => $ad->getGardenSize(),
'score' => $ad->getScore(),
'irrelevantSince' => $date,
];
}
return $scores;
}
/**
* Calculates the score for an ad based on various criteria
*
* @param Ad $ad Object of type Ad
* @return integer Final score for Ad min 0 and max 100
*/
public function calculateScore($ad): int
{
$score = 0;
$score += $this->calculateScoreForPhotos($ad);
$score += $this->calculateScoreForDescription($ad);
$score += $this->calculateScoreForSpecificWords($ad);
$score += $this->calculateScoreForCompleteness($ad);
$score = $score > 100 ? 100 : $score;
return max(0, $score);
}
/**
* Calculates the score based on the presence and quality of photos
*
* @param Ad $ad Object of type Ad
* @return integer Score for pictures quality and quantity
*/
private function calculateScoreForPhotos($ad): int
{
$score = 0;
$pictures = $ad->getPictures();
if (empty($pictures)) {
$score -= 10;
}
foreach ($pictures as $picture) {
$opicture = $this->persistence->getPictureById($picture);
$score += $opicture->isHighResolution() ? 20 : 10;
}
return $score;
}
/**
* Calculates the score based on the ad's description and typology
*
* @param Ad $ad Object of type Ad
* @return integer Score for description length
*/
private function calculateScoreForDescription($ad): int
{
$score = 0;
$descriptionBonus = 0;
$description = $ad->getDescription();
$typology = $ad->getTypology();
if (!empty($description)) {
$descriptionBonus = 5;
$wordCount = str_word_count($this->helpers->remove_accents_and_lower($description));
if ($typology === 'FLAT') {
if ($wordCount >= 20 && $wordCount <= 49) {
$score += 10;
} elseif ($wordCount >= 50) {
$score += 30;
}
} elseif ($typology === 'CHALET' && $wordCount > 50) {
$score += 20;
}
}
$score += $descriptionBonus;
return $score;
}
/**
* Calculates the score based on the ad's description and typology
*
* @param Ad $ad Object of type Ad
* @return integer Return score for specificwords
*/
private function calculateScoreForSpecificWords($ad): int
{
$score = 0;
$specificWords = ['Luminoso', 'Nuevo', 'Céntrico', 'Reformado', 'Ático'];
$wordBonus = 5;
$description = $this->helpers->remove_accents_and_lower($ad->getDescription() );
$specificWords = $this->helpers->remove_accents_and_lower($specificWords );
foreach ($specificWords as $word) {
$times = $this->helpers->calculateWordsInString($description, $word );
$score += ( $times * $wordBonus );
}
return $score;
}
/**
* Calculates the score based on the completeness of ad information
*
* @param Ad $ad Object of type Ad
* @return integer Returns score if the ad is complete
*/
private function calculateScoreForCompleteness($ad): int
{
$completenessBonus = 40;
if ($ad->getTypology() === 'GARAGE') {
if (empty($ad->getPictures()) || !$ad->getHouseSize() > 0) {
return 0;
}
return $completenessBonus;
}
if (empty($ad->getDescription())) {
return 0;
}
if (empty($ad->getPictures())) {
return 0;
}
switch ($ad->getTypology()) {
case 'FLAT':
if (!$ad->getHouseSize() > 0) {
return 0;
}
break;
case 'CHALET':
if ($ad->getHouseSize() === null || $ad->getGardenSize() === null) {
return 0;
}
break;
default:
return 0;
}
return $completenessBonus;
}
}