AbstractSaveResponse::init()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 6
nop 0
dl 0
loc 26
rs 9.3888
c 0
b 0
f 0
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
    public const MESSAGE_SUCCESS = 'save.success';
21
    public const MESSAGE_ERROR = 'save.error';
22
23
    abstract protected function getSaveDtoClass(): string;
24
25
    public function init()
26
    {
27
        $request = $this->getRequest();
28
        $saveDtoClass = $this->getSaveDtoClass();
29
        /** @var ResponseSaveDtoInterface $saveDto */
30
        $saveDto = new $saveDtoClass();
31
        $notifications = null;
32
33
        if ($request) {
34
            $requestDto = $request->getDto();
35
            if ($requestDto) {
36
                /** @var NotificationInterface $notifications */
37
                $notifications = $this->save($requestDto);
38
            }
39
        }
40
        if ($notifications && false === $notifications->hasErrorMessages()) {
41
            $saveDto->setOkStatus();
42
            $message = $this->getMessage(true);
43
        } else {
44
            $saveDto->setErrorStatus();
45
            $message = $this->getMessage(false);
46
        }
47
48
        $saveDto->setMessage($message);
49
        $saveDto->setNotification($notifications);
0 ignored issues
show
Bug introduced by
It seems like $notifications can also be of type null; however, parameter $notification of Sf4\Api\Dto\Response\Res...face::setNotification() does only seem to accept Sf4\Api\Notification\NotificationInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $saveDto->setNotification(/** @scrutinizer ignore-type */ $notifications);
Loading history...
50
        $this->setResponseDto($saveDto);
51
    }
52
53
    abstract protected function getEntityClass(): string;
54
55
    abstract protected function getEntitySaverClass(): string;
56
57
    protected function save(DtoInterface $requestDto): ?NotificationInterface
58
    {
59
        $request = $this->getRequest();
60
        if ($request) {
61
            $entityClass = $this->getEntityClass();
62
            $uuid = $request->getRequest()->attributes->get('id');
63
            $requestHandler = $request->getRequestHandler();
64
65
            $entityManager = null;
66
            if ($requestHandler) {
67
                $entityManager = $requestHandler->getEntityManager();
68
            }
69
70
            $entity = null;
71
            if ($uuid) {
72
                /** @var RepositoryInterface $repository */
73
                $repository = $entityManager->getRepository($entityClass);
74
                /** @var EntityInterface $entity */
75
                $entity = $repository->getEntityByUuid($uuid);
76
            }
77
            if (!$entity) {
78
                $entity = new $entityClass();
79
            }
80
81
            $entitySaverClass = $this->getEntitySaverClass();
82
            /** @var EntitySaverInterface $entitySaver */
83
            $entitySaver = new $entitySaverClass();
84
            $entitySaver->setEntityManager($entityManager);
0 ignored issues
show
Bug introduced by
It seems like $entityManager can also be of type null; however, parameter $entityManager of Sf4\Api\EntitySaver\Enti...ace::setEntityManager() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $entitySaver->setEntityManager(/** @scrutinizer ignore-type */ $entityManager);
Loading history...
85
            $entitySaver->setResponse($this);
86
87
            return $entitySaver->save($entity, $requestDto);
88
        }
89
90
        return null;
91
    }
92
93
    abstract protected function getMessageCodePrefix(): string;
94
95
    protected function getMessage(bool $isSuccess): string
96
    {
97
        if ($isSuccess === true) {
98
            $code = static::MESSAGE_SUCCESS;
99
        } else {
100
            $code = static::MESSAGE_ERROR;
101
        }
102
103
        $request = $this->getRequest();
104
        if ($request) {
105
            $requestHandler = $request->getRequestHandler();
106
            if ($requestHandler) {
107
                $translator = $requestHandler->getTranslator();
108
                $prefix = $this->getMessageCodePrefix();
109
                $translation = $translator->trans($prefix . $code);
110
111
                if ($translation === $prefix . $code) {
112
                    $translation = $translator->trans($code);
113
                }
114
115
                return $translation;
116
            }
117
        }
118
119
        return $code;
120
    }
121
}
122