|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Kreta\IdentityAccess\Infrastructure\Symfony\HttpAction; |
|
16
|
|
|
|
|
17
|
|
|
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException; |
|
18
|
|
|
use BenGorUser\User\Infrastructure\CommandBus\UserCommandBus; |
|
19
|
|
|
use BenGorUser\UserBundle\Form\FormErrorSerializer; |
|
20
|
|
|
use Kreta\IdentityAccess\Domain\Model\User\UserEmailAlreadyExistsException; |
|
21
|
|
|
use Kreta\IdentityAccess\Domain\Model\User\UsernameAlreadyExistsException; |
|
22
|
|
|
use Kreta\IdentityAccess\Infrastructure\Symfony\Form\Type\EditProfileType; |
|
23
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface; |
|
24
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
27
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
28
|
|
|
|
|
29
|
|
|
class EditProfileAction |
|
30
|
|
|
{ |
|
31
|
|
|
private $tokenStorage; |
|
32
|
|
|
private $formFactory; |
|
33
|
|
|
private $commandBus; |
|
34
|
|
|
private $encoder; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
TokenStorageInterface $tokenStorage, |
|
38
|
|
|
FormFactoryInterface $formFactory, |
|
39
|
|
|
UserCommandBus $commandBus, |
|
40
|
|
|
JWTEncoderInterface $encoder |
|
41
|
|
|
) { |
|
42
|
|
|
$this->formFactory = $formFactory; |
|
43
|
|
|
$this->commandBus = $commandBus; |
|
44
|
|
|
$this->tokenStorage = $tokenStorage; |
|
45
|
|
|
$this->encoder = $encoder; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function __invoke(Request $request) : JsonResponse |
|
49
|
|
|
{ |
|
50
|
|
|
$userId = $this->tokenStorage->getToken()->getUser()->id; |
|
51
|
|
|
|
|
52
|
|
|
$form = $this->formFactory->createNamed('', EditProfileType::class, null, ['user_id' => $userId]); |
|
53
|
|
|
$form->handleRequest($request); |
|
54
|
|
|
|
|
55
|
|
|
if ($form->isValid()) { |
|
56
|
|
|
$data = $form->getData(); |
|
57
|
|
|
try { |
|
58
|
|
|
$this->commandBus->handle($data); |
|
59
|
|
|
$token = $this->encoder->encode(['email' => $data->email()]); |
|
60
|
|
|
|
|
61
|
|
|
return new JsonResponse([ |
|
62
|
|
|
'user_id' => $data->id(), |
|
63
|
|
|
'email' => $data->email(), |
|
64
|
|
|
'username' => $data->userName(), |
|
65
|
|
|
'token' => $token, |
|
66
|
|
|
'first_name' => $data->firstName(), |
|
67
|
|
|
'last_name' => $data->lastName(), |
|
68
|
|
|
]); |
|
69
|
|
|
} catch (UserDoesNotExistException $exception) { |
|
70
|
|
|
return new JsonResponse( |
|
71
|
|
|
sprintf( |
|
72
|
|
|
'Does not exist any user with "%s" user id.', |
|
73
|
|
|
$data->id() |
|
74
|
|
|
), 400); |
|
75
|
|
|
} catch (UserEmailAlreadyExistsException $exception) { |
|
76
|
|
|
return new JsonResponse( |
|
77
|
|
|
sprintf( |
|
78
|
|
|
'The given "%s" email is already in use', |
|
79
|
|
|
$data->email() |
|
80
|
|
|
), 400); |
|
81
|
|
|
} catch (UsernameAlreadyExistsException $exception) { |
|
82
|
|
|
return new JsonResponse( |
|
83
|
|
|
sprintf( |
|
84
|
|
|
'The given "%s" username is already in use', |
|
85
|
|
|
$data->username() |
|
86
|
|
|
), 400); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return new JsonResponse(FormErrorSerializer::errors($form), 400); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|