|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Client\GmailApiClient; |
|
6
|
|
|
use App\Entity\User; |
|
7
|
|
|
use App\Enum\PropertyFilter; |
|
8
|
|
|
use App\Exception\GmailApiException; |
|
9
|
|
|
use App\Exception\GoogleException; |
|
10
|
|
|
use App\Exception\GoogleInsufficientPermissionException; |
|
11
|
|
|
use App\Exception\GoogleRefreshTokenException; |
|
12
|
|
|
use App\Service\GoogleOAuthService; |
|
13
|
|
|
use App\Service\PropertyService; |
|
14
|
|
|
use App\Service\PropertySortResolver; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Google\Service\Gmail\Label; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
22
|
|
|
|
|
23
|
|
|
#[Route("/property")] |
|
24
|
|
|
class PropertyController extends AbstractController |
|
25
|
|
|
{ |
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private GmailApiClient $gmailClient, |
|
28
|
|
|
private GoogleOAuthService $googleOAuthService, |
|
29
|
|
|
private PropertyService $propertyService, |
|
30
|
|
|
private PropertySortResolver $sortResolver, |
|
31
|
|
|
private LoggerInterface $logger |
|
32
|
|
|
) {} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @throws GmailApiException|GoogleException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function index(): Response |
|
38
|
|
|
{ |
|
39
|
|
|
/** @var User $user */ |
|
40
|
|
|
$user = $this->getUser(); |
|
41
|
|
|
|
|
42
|
|
|
// Get a valid access token |
|
43
|
|
|
try { |
|
44
|
|
|
$accessToken = $this->googleOAuthService->refreshAccessTokenIfExpired($user); |
|
45
|
|
|
} catch (GoogleRefreshTokenException $e) { |
|
46
|
|
|
$this->logger->error('Failed to refresh the access token: ' . $e->getMessage()); |
|
47
|
|
|
|
|
48
|
|
|
return $this->redirectToRoute('app_logout'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
$labels = $this->gmailClient->getLabels($accessToken); |
|
53
|
|
|
} catch (GoogleInsufficientPermissionException $e) { |
|
54
|
|
|
$this->logger->error('Failed to get the Gmail labels: ' . $e->getMessage()); |
|
55
|
|
|
|
|
56
|
|
|
return $this->redirectToRoute('app_logout'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$settings = $user->getPropertySearchSettings(); |
|
60
|
|
|
|
|
61
|
|
|
return $this->render('property/index.html.twig', [ |
|
62
|
|
|
'gmail_label_choices' => $this->getGmailLabelChoices($labels), |
|
63
|
|
|
'newer_than' => $settings[PropertyFilter::NEWER_THAN] ?? null, |
|
64
|
|
|
'gmail_label' => $settings[PropertyFilter::GMAIL_LABEL] ?? null, |
|
65
|
|
|
'provider' => $settings[PropertyFilter::PROVIDER] ?? null, |
|
66
|
|
|
'new_build' => isset($settings[PropertyFilter::NEW_BUILD]), |
|
67
|
|
|
'sort' => $settings['sort'] ?? null |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
#[Route("/list", name: "property_list", options: ["expose" => true], methods: ["GET"])] |
|
72
|
|
|
public function list(Request $request): Response |
|
73
|
|
|
{ |
|
74
|
|
|
parse_str($request->query->get('filters'), $filters); |
|
75
|
|
|
$sort = $request->query->get('sort'); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($filters[PropertyFilter::NEW_BUILD])) { |
|
78
|
|
|
$filters[PropertyFilter::NEW_BUILD] = true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** @var User $user */ |
|
82
|
|
|
$user = $this->getUser(); |
|
83
|
|
|
$user->setPropertySearchSettings(array_merge($filters, ['sort' => $sort])); |
|
84
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$properties = $this->propertyService->find($user, $filters, $this->sortResolver->resolve($sort)); |
|
88
|
|
|
} catch (GoogleRefreshTokenException|GoogleInsufficientPermissionException $e) { |
|
89
|
|
|
$this->logger->error('Failed to get the Gmail messages: ' . $e->getMessage()); |
|
90
|
|
|
|
|
91
|
|
|
return $this->redirectToRoute('app_logout'); |
|
92
|
|
|
} catch (Exception $e) { |
|
93
|
|
|
$this->addFlash('error', 'Erreur lors de la récupération des biens immobiliers: ' . $e->getMessage()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->render('property/_list.html.twig', [ |
|
97
|
|
|
'properties' => $properties ?? [], |
|
98
|
|
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Label[] $labels |
|
103
|
|
|
*/ |
|
104
|
|
|
private function getGmailLabelChoices(array $labels): array |
|
105
|
|
|
{ |
|
106
|
|
|
$choices = []; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($labels as $label) { |
|
109
|
|
|
$choices[$label->name] = $label->id; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $choices; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|