|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 6.02.19 |
|
6
|
|
|
* Time: 8:02 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\Response; |
|
10
|
|
|
|
|
11
|
|
|
use Sf4\Api\Dto\DtoInterface; |
|
12
|
|
|
use Sf4\Api\Dto\Response\ResponseSaveDtoInterface; |
|
13
|
|
|
use Sf4\Api\Entity\EntityInterface; |
|
14
|
|
|
use Sf4\Api\EntitySaver\EntitySaverInterface; |
|
15
|
|
|
use Sf4\Api\Notification\NotificationInterface; |
|
16
|
|
|
use Sf4\Api\Repository\RepositoryInterface; |
|
17
|
|
|
|
|
18
|
|
|
abstract class AbstractSaveResponse extends AbstractResponse |
|
19
|
|
|
{ |
|
20
|
|
|
const MESSAGE_SUCCESS = 'save.success'; |
|
21
|
|
|
const MESSAGE_ERROR = 'save.error'; |
|
22
|
|
|
|
|
23
|
|
|
abstract protected function getSaveDtoClass(): string; |
|
24
|
|
|
|
|
25
|
|
|
public function init() |
|
26
|
|
|
{ |
|
27
|
|
|
$requestDto = $this->getRequest()->getDto(); |
|
28
|
|
|
$saveDtoClass = $this->getSaveDtoClass(); |
|
29
|
|
|
/** @var ResponseSaveDtoInterface $saveDto */ |
|
30
|
|
|
$saveDto = new $saveDtoClass(); |
|
31
|
|
|
|
|
32
|
|
|
/** @var NotificationInterface $notifications */ |
|
33
|
|
|
$notifications = $this->save($requestDto); |
|
|
|
|
|
|
34
|
|
|
if ($notifications->hasErrorMessages()) { |
|
35
|
|
|
$saveDto->setOkStatus(); |
|
36
|
|
|
$message = $this->getMessage(true); |
|
37
|
|
|
} else { |
|
38
|
|
|
$saveDto->setErrorStatus(); |
|
39
|
|
|
$message = $this->getMessage(false); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$saveDto->setMessage($message); |
|
43
|
|
|
$this->setResponseDto($saveDto); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
abstract protected function getEntityClass(): string; |
|
47
|
|
|
|
|
48
|
|
|
abstract protected function getEntitySaverClass(): string; |
|
49
|
|
|
|
|
50
|
|
|
protected function save(DtoInterface $requestDto): NotificationInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$entityManager = $this->getRequest()->getRequestHandler()->getEntityManager(); |
|
53
|
|
|
$entityClass = $this->getEntityClass(); |
|
54
|
|
|
$entity = null; |
|
55
|
|
|
$uuid = $this->getRequest()->getRequest()->attributes->get('id'); |
|
56
|
|
|
if ($uuid) { |
|
57
|
|
|
/** @var RepositoryInterface $repository */ |
|
58
|
|
|
$repository = $entityManager->getRepository($entityClass); |
|
59
|
|
|
/** @var EntityInterface $entity */ |
|
60
|
|
|
$entity = $repository->getEntityByUuid($uuid); |
|
61
|
|
|
} |
|
62
|
|
|
if (!$entity) { |
|
63
|
|
|
$entity = new $entityClass(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$entitySaverClass = $this->getEntitySaverClass(); |
|
67
|
|
|
/** @var EntitySaverInterface $entitySaver */ |
|
68
|
|
|
$entitySaver = new $entitySaverClass(); |
|
69
|
|
|
$entitySaver->setEntityManager($entityManager); |
|
70
|
|
|
|
|
71
|
|
|
return $entitySaver->save($entity, $requestDto); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
abstract protected function getMessageCodePrefix(): string; |
|
75
|
|
|
|
|
76
|
|
|
protected function getMessage(bool $isSuccess) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($isSuccess === true) { |
|
79
|
|
|
$code = static::MESSAGE_SUCCESS; |
|
80
|
|
|
} else { |
|
81
|
|
|
$code = static::MESSAGE_ERROR; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$prefix = $this->getMessageCodePrefix(); |
|
85
|
|
|
$translator = $this->getRequest()->getRequestHandler()->getTranslator(); |
|
86
|
|
|
$translation = $translator->trans($prefix . $code); |
|
87
|
|
|
|
|
88
|
|
|
if ($translation === $prefix . $code) { |
|
89
|
|
|
$translation = $translator->trans($code); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $translation; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|