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