Passed
Push — master ( 7a68ca...706354 )
by Peter
08:27 queued 01:17
created

FormAbstract::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Http\Controllers\Admin;
6
7
use AbterPhp\Admin\Form\Factory\IFormFactory;
8
use AbterPhp\Framework\Constant\Event;
9
use AbterPhp\Framework\Constant\Session;
10
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
11
use AbterPhp\Framework\Events\FormReady;
12
use AbterPhp\Framework\I18n\ITranslator;
13
use AbterPhp\Framework\Orm\IGridRepo;
14
use AbterPhp\Framework\Session\FlashService;
15
use Casbin\Exceptions\CasbinException;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Http\Requests\RequestMethods;
18
use Opulence\Http\Responses\Response;
19
use Opulence\Orm\OrmException;
20
use Opulence\Routing\Urls\URLException;
21
use Opulence\Routing\Urls\UrlGenerator;
22
use Opulence\Sessions\ISession;
23
use Psr\Log\LoggerInterface;
24
25
abstract class FormAbstract extends AdminAbstract
26
{
27
    const LOG_MSG_LOAD_FAILURE = 'Loading %1$s failed.';
28
29
    const ENTITY_TITLE_SINGULAR = '';
30
31
    const VIEW_FORM = 'contents/backend/form';
32
33
    const VAR_ENTITY = 'entity';
34
    const VAR_FORM   = 'form';
35
36
    const TITLE_NEW  = 'framework:titleNew';
37
    const TITLE_EDIT = 'framework:titleEdit';
38
39
    const URL_NEW = '%s-new';
40
41
    const RESOURCE_DEFAULT = '%s-form';
42
    const RESOURCE_HEADER  = '%s-header-form';
43
    const RESOURCE_FOOTER  = '%s-footer-form';
44
    const RESOURCE_TYPE    = 'form';
45
46
    /** @var IGridRepo */
47
    protected $repo;
48
49
    /** @var ISession */
50
    protected $session;
51
52
    /** @var IFormFactory */
53
    protected $formFactory;
54
55
    /** @var IEventDispatcher */
56
    protected $eventDispatcher;
57
58
    /**
59
     * FormAbstract constructor.
60
     *
61
     * @param FlashService     $flashService
62
     * @param ITranslator      $translator
63
     * @param UrlGenerator     $urlGenerator
64
     * @param LoggerInterface  $logger
65
     * @param IGridRepo        $repo
66
     * @param ISession         $session
67
     * @param IFormFactory     $formFactory
68
     * @param IEventDispatcher $eventDispatcher
69
     */
70
    public function __construct(
71
        FlashService $flashService,
72
        ITranslator $translator,
73
        UrlGenerator $urlGenerator,
74
        LoggerInterface $logger,
75
        IGridRepo $repo,
76
        ISession $session,
77
        IFormFactory $formFactory,
78
        IEventDispatcher $eventDispatcher
79
    ) {
80
        parent::__construct($flashService, $translator, $urlGenerator, $logger);
81
82
        $this->repo            = $repo;
83
        $this->session         = $session;
84
        $this->formFactory     = $formFactory;
85
        $this->eventDispatcher = $eventDispatcher;
86
    }
87
88
    /**
89
     * @return Response
90
     * @throws CasbinException
91
     * @throws URLException
92
     * @throws \Throwable
93
     */
94
    public function new(): Response
95
    {
96
        $entity = $this->createEntity('');
97
98
        $url   = $this->urlGenerator->createFromName(sprintf(static::URL_NEW, static::ENTITY_PLURAL));
99
        $title = $this->translator->translate(static::TITLE_NEW, static::ENTITY_TITLE_SINGULAR);
100
        $form  = $this->formFactory->create($url, RequestMethods::POST, $this->getShowUrl(), $entity);
101
102
        $form->setTranslator($this->translator);
103
104
        $this->eventDispatcher->dispatch(Event::FORM_READY, new FormReady($form));
105
106
        $this->view = $this->viewFactory->createView(static::VIEW_FORM);
107
        $this->view->setVar(static::VAR_ENTITY, $entity);
108
        $this->view->setVar(static::VAR_FORM, $form);
109
110
        $this->addCustomAssets($entity);
111
112
        return $this->createResponse($title);
113
    }
114
115
    /**
116
     * @param string $entityId
117
     *
118
     * @return Response
119
     * @throws CasbinException
120
     * @throws URLException
121
     * @throws \Throwable
122
     */
123
    public function edit(string $entityId): Response
124
    {
125
        $entity = $this->retrieveEntity($entityId);
126
127
        $url   = $this->getEditUrl($entityId);
128
        $title = $this->translator->translate(static::TITLE_EDIT, static::ENTITY_TITLE_SINGULAR, (string)$entity);
129
        $form  = $this->formFactory->create($url, RequestMethods::PUT, $this->getShowUrl(), $entity);
130
131
        $this->eventDispatcher->dispatch(Event::FORM_READY, new FormReady($form));
132
133
        $form->setTranslator($this->translator);
134
135
        $this->view = $this->viewFactory->createView(sprintf(static::VIEW_FORM, strtolower(static::ENTITY_SINGULAR)));
136
        $this->view->setVar(static::VAR_ENTITY, $entity);
137
        $this->view->setVar(static::VAR_FORM, $form);
138
139
        $this->addCustomAssets($entity);
140
141
        return $this->createResponse($title);
142
    }
143
144
    /**
145
     * @param string $entityId
146
     *
147
     * @return IStringerEntity
148
     */
149
    public function retrieveEntity(string $entityId): IStringerEntity
150
    {
151
        /** @var FlashService $flashService */
152
        $flashService = $this->flashService;
153
154
        try {
155
            /** @var IStringerEntity $entity */
156
            $entity = $this->repo->getById($entityId);
157
        } catch (OrmException $e) {
158
            $errorMessage = $this->getMessage(static::ENTITY_LOAD_FAILURE);
159
160
            $flashService->mergeErrorMessages([$errorMessage]);
161
162
            $this->logger->info(
163
                sprintf(static::LOG_MSG_LOAD_FAILURE, static::ENTITY_SINGULAR),
164
                $this->getExceptionContext($e)
165
            );
166
167
            return $this->createEntity('');
168
        }
169
170
        return $entity;
171
    }
172
173
    /**
174
     * @param string $entityEntityId
175
     *
176
     * @return IStringerEntity
177
     */
178
    abstract protected function createEntity(string $entityEntityId): IStringerEntity;
179
180
    /**
181
     * @return string
182
     * @throws URLException
183
     */
184
    protected function getShowUrl(): string
185
    {
186
        if ($this->session->has(Session::LAST_GRID_URL)) {
187
            return (string)$this->session->get(Session::LAST_GRID_URL);
188
        }
189
190
        $url = $this->urlGenerator->createFromName(static::ROUTING_PATH);
191
192
        return $url;
193
    }
194
195
    /**
196
     * @param string $id
197
     *
198
     * @return string
199
     * @throws URLException
200
     */
201
    protected function getEditUrl(string $id): string
202
    {
203
        $routeName = sprintf(static::URL_EDIT, static::ROUTING_PATH);
204
205
        $url = $this->urlGenerator->createFromName($routeName, $id);
206
207
        return $url;
208
    }
209
210
    /**
211
     * @param string $messageType
212
     *
213
     * @return string
214
     */
215
    protected function getMessage(string $messageType)
216
    {
217
        $entityName = $this->translator->translate(static::ENTITY_TITLE_SINGULAR);
218
219
        return $this->translator->translate($messageType, $entityName);
220
    }
221
}
222