1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\User\Controllers; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
8
|
|
|
use VSV\GVQ_API\Company\Models\Company; |
9
|
|
|
use VSV\GVQ_API\Company\Repositories\CompanyRepository; |
10
|
|
|
use VSV\GVQ_API\User\Models\LoginDetails; |
11
|
|
|
use VSV\GVQ_API\User\Repositories\UserRepository; |
12
|
|
|
|
13
|
|
|
class UserAccountController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var UserRepository |
17
|
|
|
*/ |
18
|
|
|
private $userRepository; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CompanyRepository |
22
|
|
|
*/ |
23
|
|
|
private $companyRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SerializerInterface |
27
|
|
|
*/ |
28
|
|
|
private $serializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param UserRepository $userRepository |
32
|
|
|
* @param CompanyRepository $companyRepository |
33
|
|
|
* @param SerializerInterface $serializer |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
UserRepository $userRepository, |
37
|
|
|
CompanyRepository $companyRepository, |
38
|
|
|
SerializerInterface $serializer |
39
|
|
|
) { |
40
|
|
|
$this->userRepository = $userRepository; |
41
|
|
|
$this->companyRepository = $companyRepository; |
42
|
|
|
$this->serializer = $serializer; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Request $request |
48
|
|
|
* @return Response |
49
|
|
|
*/ |
50
|
|
|
public function login(Request $request): Response |
51
|
|
|
{ |
52
|
|
|
$json = $request->getContent(); |
53
|
|
|
$loginDetails = new LoginDetails(json_decode($json, true)); |
54
|
|
|
|
55
|
|
|
$user = $this->userRepository->getByEmail($loginDetails->getEmail()); |
56
|
|
|
if ($user === null) { |
57
|
|
|
throw new \InvalidArgumentException('Login failed.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$user->getPassword() || |
61
|
|
|
!$user->getPassword()->verifies($loginDetails->getPassword())) { |
62
|
|
|
throw new \InvalidArgumentException('Login failed.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$response = new Response( |
66
|
|
|
$this->serializer->serialize($user, 'json') |
67
|
|
|
); |
68
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
69
|
|
|
|
70
|
|
|
return $response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Request $request |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
|
|
public function register(Request $request): Response |
78
|
|
|
{ |
79
|
|
|
$json = $request->getContent(); |
80
|
|
|
/** @var Company $company */ |
81
|
|
|
$company = $this->serializer->deserialize( |
82
|
|
|
$json, |
83
|
|
|
Company::class, |
84
|
|
|
'json', |
85
|
|
|
[ |
86
|
|
|
'role' => 'contact' |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$user = $company->getUser(); |
91
|
|
|
$this->userRepository->save($user); |
92
|
|
|
|
93
|
|
|
$this->companyRepository->save($company); |
94
|
|
|
|
95
|
|
|
$response = new Response('{"id":"'.$user->getId()->toString().'"}'); |
96
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
97
|
|
|
|
98
|
|
|
return $response; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|