Completed
Push — master ( b5de62...4c3851 )
by
unknown
19s queued 11s
created

BusinessUnitController::deleteAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
c 0
b 0
f 0
dl 0
loc 35
rs 8.9777
cc 6
nc 6
nop 1
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerShop\Yves\CompanyPage\Controller;
9
10
use Generated\Shared\Transfer\CompanyBusinessUnitCriteriaFilterTransfer;
11
use Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer;
12
use Generated\Shared\Transfer\CompanyBusinessUnitTransfer;
13
use Generated\Shared\Transfer\CompanyUnitAddressCriteriaFilterTransfer;
14
use SprykerShop\Yves\CompanyPage\Form\CompanyBusinessUnitForm;
15
use SprykerShop\Yves\CompanyPage\Plugin\Provider\CompanyPageControllerProvider;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
19
/**
20
 * @method \SprykerShop\Yves\CompanyPage\CompanyPageFactory getFactory()
21
 */
22
class BusinessUnitController extends AbstractCompanyController
23
{
24
    protected const MESSAGE_FORM_CSRF_VALIDATION_ERROR = 'form.csrf.error.text';
25
    protected const BUSINESS_UNIT_LIST_SORT_FIELD = 'id_company_business_unit';
26
    protected const COMPANY_UNIT_ADDRESS_LIST_SORT_FIELD = 'id_company_unit_address';
27
    protected const REQUEST_PARAM_ID = 'id';
28
29
    /**
30
     * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse
31
     */
32
    public function indexAction()
33
    {
34
        $viewData = $this->executeIndexAction();
35
36
        return $this->view($viewData, [], '@CompanyPage/views/business-unit/business-unit.twig');
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    protected function executeIndexAction(): array
43
    {
44
        return [
45
            'businessUnitsTree' => $this->getFactory()->createCompanyBusinessUnitTreeBuilder()->getCustomerCompanyBusinessUnitTree(),
46
        ];
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     *
52
     * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse
53
     */
54
    public function detailsAction(Request $request)
55
    {
56
        $viewData = $this->getCompanyBusinessUnitDetailsResponseData($request);
57
58
        return $this->view($viewData, [], '@CompanyPage/views/business-unit-detail/business-unit-detail.twig');
59
    }
60
61
    /**
62
     * @param \Symfony\Component\HttpFoundation\Request $request
63
     *
64
     * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse
65
     */
66
    public function createAction(Request $request)
67
    {
68
        $response = $this->executeCreateAction($request);
69
70
        if (!is_array($response)) {
71
            return $response;
72
        }
73
74
        return $this->view($response, [], '@CompanyPage/views/business-unit-create/business-unit-create.twig');
75
    }
76
77
    /**
78
     * @param \Symfony\Component\HttpFoundation\Request $request
79
     *
80
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
81
     */
82
    protected function executeCreateAction(Request $request)
83
    {
84
        $dataProvider = $this->getFactory()
85
            ->createCompanyPageFormFactory()
86
            ->createBusinessUnitFormDataProvider();
87
88
        $companyUserTransfer = $this->findCurrentCompanyUserTransfer();
89
        $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID);
90
        $dataProviderOptions = $dataProvider->getOptions($companyUserTransfer, $idCompanyBusinessUnit);
0 ignored issues
show
Bug introduced by
It seems like $companyUserTransfer can also be of type null; however, parameter $companyUserTransfer of SprykerShop\Yves\Company...aProvider::getOptions() does only seem to accept Generated\Shared\Transfer\CompanyUserTransfer, 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

90
        $dataProviderOptions = $dataProvider->getOptions(/** @scrutinizer ignore-type */ $companyUserTransfer, $idCompanyBusinessUnit);
Loading history...
91
92
        $companyBusinessUnitForm = $this->getFactory()
93
            ->createCompanyPageFormFactory()
94
            ->getBusinessUnitForm($dataProviderOptions)
95
            ->handleRequest($request);
96
97
        if ($companyBusinessUnitForm->isSubmitted() === false) {
98
            $companyBusinessUnitForm->setData($dataProvider->getData($this->findCurrentCompanyUserTransfer()));
0 ignored issues
show
Bug introduced by
It seems like $this->findCurrentCompanyUserTransfer() can also be of type null; however, parameter $companyUserTransfer of SprykerShop\Yves\Company...DataProvider::getData() does only seem to accept Generated\Shared\Transfer\CompanyUserTransfer, 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

98
            $companyBusinessUnitForm->setData($dataProvider->getData(/** @scrutinizer ignore-type */ $this->findCurrentCompanyUserTransfer()));
Loading history...
99
        }
100
101
        if ($companyBusinessUnitForm->isSubmitted() === true && $companyBusinessUnitForm->isValid() === true) {
102
            $companyBusinessUnitResponseTransfer = $this->companyBusinessUnitSave($companyBusinessUnitForm->getData());
103
104
            if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) {
105
                $this->applySuccessMessage($companyBusinessUnitResponseTransfer);
106
            }
107
108
            if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) {
109
                $this->applyErrorMessage($companyBusinessUnitResponseTransfer);
110
            }
111
112
            return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT_UPDATE, [
113
                'id' => $companyBusinessUnitResponseTransfer->getCompanyBusinessUnitTransfer()->getIdCompanyBusinessUnit(),
114
            ]);
115
        }
116
117
        return [
118
            'form' => $companyBusinessUnitForm->createView(),
119
        ];
120
    }
121
122
    /**
123
     * @param \Symfony\Component\HttpFoundation\Request $request
124
     *
125
     * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse
126
     */
127
    public function updateAction(Request $request)
128
    {
129
        $response = $this->executeUpdateAction($request);
130
131
        if (!is_array($response)) {
132
            return $response;
133
        }
134
135
        return $this->view($response, [], '@CompanyPage/views/business-unit-update/business-unit-update.twig');
136
    }
137
138
    /**
139
     * @param \Symfony\Component\HttpFoundation\Request $request
140
     *
141
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
142
     *
143
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
144
     */
145
    protected function executeUpdateAction(Request $request)
146
    {
147
        $dataProvider = $this->getFactory()
148
            ->createCompanyPageFormFactory()
149
            ->createBusinessUnitFormDataProvider();
150
151
        $companyUserTransfer = $this->findCurrentCompanyUserTransfer();
152
        $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID);
153
        $dataProviderOptions = $dataProvider->getOptions($companyUserTransfer, $idCompanyBusinessUnit);
0 ignored issues
show
Bug introduced by
It seems like $companyUserTransfer can also be of type null; however, parameter $companyUserTransfer of SprykerShop\Yves\Company...aProvider::getOptions() does only seem to accept Generated\Shared\Transfer\CompanyUserTransfer, 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

153
        $dataProviderOptions = $dataProvider->getOptions(/** @scrutinizer ignore-type */ $companyUserTransfer, $idCompanyBusinessUnit);
Loading history...
154
155
        $companyBusinessUnitForm = $this->getFactory()
156
            ->createCompanyPageFormFactory()
157
            ->getBusinessUnitForm($dataProviderOptions)
158
            ->handleRequest($request);
159
160
        if ($companyBusinessUnitForm->isSubmitted() === false) {
161
            $data = $dataProvider->getData($this->findCurrentCompanyUserTransfer(), $idCompanyBusinessUnit);
0 ignored issues
show
Bug introduced by
It seems like $this->findCurrentCompanyUserTransfer() can also be of type null; however, parameter $companyUserTransfer of SprykerShop\Yves\Company...DataProvider::getData() does only seem to accept Generated\Shared\Transfer\CompanyUserTransfer, 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

161
            $data = $dataProvider->getData(/** @scrutinizer ignore-type */ $this->findCurrentCompanyUserTransfer(), $idCompanyBusinessUnit);
Loading history...
162
163
            $companyBusinessUnitTransfer = $this->getFactory()
164
                ->getCompanyBusinessUnitClient()
165
                ->getCompanyBusinessUnitById((new CompanyBusinessUnitTransfer())->setIdCompanyBusinessUnit($idCompanyBusinessUnit));
166
167
            if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) {
168
                throw new NotFoundHttpException();
169
            }
170
171
            $companyBusinessUnitForm->setData($data);
172
        }
173
174
        if ($companyBusinessUnitForm->isSubmitted() === true && $companyBusinessUnitForm->isValid() === true) {
175
            $companyBusinessUnitResponseTransfer = $this->companyBusinessUnitSave($companyBusinessUnitForm->getData());
176
177
            if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) {
178
                $this->applySuccessMessage($companyBusinessUnitResponseTransfer);
179
            }
180
181
            if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) {
182
                $this->applyErrorMessage($companyBusinessUnitResponseTransfer);
183
            }
184
185
            return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT_UPDATE, [
186
                'id' => $idCompanyBusinessUnit,
187
            ]);
188
        }
189
190
        return [
191
            'form' => $companyBusinessUnitForm->createView(),
192
            'addresses' => $dataProviderOptions[CompanyBusinessUnitForm::FIELD_COMPANY_UNIT_ADDRESSES],
193
        ];
194
    }
195
196
    /**
197
     * @param \Symfony\Component\HttpFoundation\Request $request
198
     *
199
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
200
     *
201
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
202
     */
203
    public function deleteAction(Request $request)
204
    {
205
        $companyBusinessUnitDeleteForm = $this->getFactory()->createCompanyPageFormFactory()->getCompanyBusinessUnitDeleteForm()->handleRequest($request);
206
207
        if (!$companyBusinessUnitDeleteForm->isSubmitted() || !$companyBusinessUnitDeleteForm->isValid()) {
208
            $this->addErrorMessage(static::MESSAGE_FORM_CSRF_VALIDATION_ERROR);
209
210
            return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT);
211
        }
212
213
        $companyBusinessUnitId = $request->query->getInt(static::REQUEST_PARAM_ID);
214
        $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer();
215
        $companyBusinessUnitTransfer->setIdCompanyBusinessUnit($companyBusinessUnitId);
216
217
        $companyBusinessUnitTransfer = $this->getFactory()
218
            ->getCompanyBusinessUnitClient()
219
            ->getCompanyBusinessUnitById($companyBusinessUnitTransfer);
220
221
        if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) {
222
            throw new NotFoundHttpException();
223
        }
224
225
        $companyBusinessUnitResponseTransfer = $this->getFactory()
226
            ->getCompanyBusinessUnitClient()
227
            ->deleteCompanyBusinessUnit($companyBusinessUnitTransfer);
228
229
        if ($companyBusinessUnitResponseTransfer->getIsSuccessful()) {
230
            $this->applySuccessMessage($companyBusinessUnitResponseTransfer);
231
        }
232
233
        if (!$companyBusinessUnitResponseTransfer->getIsSuccessful()) {
234
            $this->applyErrorMessage($companyBusinessUnitResponseTransfer);
235
        }
236
237
        return $this->redirectResponseInternal(CompanyPageControllerProvider::ROUTE_COMPANY_BUSINESS_UNIT);
238
    }
239
240
    /**
241
     * @param \Symfony\Component\HttpFoundation\Request $request
242
     *
243
     * @return array|\Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse
244
     */
245
    public function confirmDeleteAction(Request $request)
246
    {
247
        $viewData = $this->executeConfirmDeleteAction($request);
248
249
        return $this->view(
250
            $viewData,
251
            [],
252
            '@CompanyPage/views/business-unit-delete-confirmation-page/business-unit-delete-confirmation-page.twig'
253
        );
254
    }
255
256
    /**
257
     * @param \Symfony\Component\HttpFoundation\Request $request
258
     *
259
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
260
     *
261
     * @return array
262
     */
263
    protected function executeConfirmDeleteAction(Request $request): array
264
    {
265
        $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID);
266
        $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer();
267
        $companyBusinessUnitTransfer->setIdCompanyBusinessUnit($idCompanyBusinessUnit);
268
269
        $companyBusinessUnitTransfer = $this->getFactory()
270
            ->getCompanyBusinessUnitClient()
271
            ->getCompanyBusinessUnitById($companyBusinessUnitTransfer);
272
273
        if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) {
274
            throw new NotFoundHttpException();
275
        }
276
277
        return [
278
            'companyBusinessUnitDeleteForm' => $this->getFactory()->createCompanyPageFormFactory()->getCompanyBusinessUnitDeleteForm()->createView(),
279
            'companyBusinessUnit' => $companyBusinessUnitTransfer,
280
        ];
281
    }
282
283
    /**
284
     * @param \Symfony\Component\HttpFoundation\Request $request
285
     *
286
     * @return \Generated\Shared\Transfer\CompanyBusinessUnitCriteriaFilterTransfer
287
     */
288
    protected function createBusinessUnitCriteriaFilterTransfer(Request $request): CompanyBusinessUnitCriteriaFilterTransfer
289
    {
290
        $criteriaFilterTransfer = new CompanyBusinessUnitCriteriaFilterTransfer();
291
292
        $criteriaFilterTransfer->setIdCompany($this->findCurrentCompanyUserTransfer()->getFkCompany());
293
294
        $filterTransfer = $this->createFilterTransfer(self::BUSINESS_UNIT_LIST_SORT_FIELD);
295
        $criteriaFilterTransfer->setFilter($filterTransfer);
296
297
        $paginationTransfer = $this->createPaginationTransfer($request);
298
        $criteriaFilterTransfer->setPagination($paginationTransfer);
299
300
        return $criteriaFilterTransfer;
301
    }
302
303
    /**
304
     * @param \Symfony\Component\HttpFoundation\Request $request
305
     *
306
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
307
     *
308
     * @return array
309
     */
310
    protected function getCompanyBusinessUnitDetailsResponseData(Request $request): array
311
    {
312
        $idCompanyBusinessUnit = $request->query->getInt(static::REQUEST_PARAM_ID);
313
        $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer();
314
        $companyBusinessUnitTransfer->setIdCompanyBusinessUnit($idCompanyBusinessUnit);
315
316
        $companyBusinessUnitTransfer = $this->getFactory()->getCompanyBusinessUnitClient()
317
            ->getCompanyBusinessUnitById($companyBusinessUnitTransfer);
318
319
        if (!$this->isCurrentCustomerRelatedToCompany($companyBusinessUnitTransfer->getFkCompany())) {
320
            throw new NotFoundHttpException();
321
        }
322
323
        $criteriaFilterTransfer = $this->createCompanyUnitAddressCriteriaFilterTransfer($request);
324
        $criteriaFilterTransfer->setIdCompanyBusinessUnit($idCompanyBusinessUnit);
325
        $criteriaFilterTransfer = $this->getFactory()
326
            ->getCompanyUnitAddressClient()
327
            ->getCompanyUnitAddressCollection($criteriaFilterTransfer);
328
        $addresses = $criteriaFilterTransfer->getCompanyUnitAddresses();
329
330
        foreach ($addresses as &$address) {
331
            if ($address->getIdCompanyUnitAddress() === $companyBusinessUnitTransfer->getDefaultBillingAddress()) {
332
                $address->setIsDefaultBilling(true);
333
            }
334
        }
335
336
        return [
337
            'addresses' => $addresses,
338
            'pagination' => $criteriaFilterTransfer->getPagination(),
339
            'businessUnit' => $companyBusinessUnitTransfer,
340
            'companyUnitAddressDeleteFormCloner' => $this->getFactory()->createFormCloner()
341
                ->setForm($this->getFactory()->createCompanyPageFormFactory()->getCompanyUnitAddressDeleteForm()),
342
        ];
343
    }
344
345
    /**
346
     * @param \Symfony\Component\HttpFoundation\Request $request
347
     *
348
     * @return \Generated\Shared\Transfer\CompanyUnitAddressCriteriaFilterTransfer
349
     */
350
    protected function createCompanyUnitAddressCriteriaFilterTransfer(
351
        Request $request
352
    ): CompanyUnitAddressCriteriaFilterTransfer {
353
        $criteriaFilterTransfer = new CompanyUnitAddressCriteriaFilterTransfer();
354
        $criteriaFilterTransfer->setIdCompany($this->findCurrentCompanyUserTransfer()->getFkCompany());
355
356
        $filterTransfer = $this->createFilterTransfer(self::COMPANY_UNIT_ADDRESS_LIST_SORT_FIELD);
357
        $criteriaFilterTransfer->setFilter($filterTransfer);
358
359
        $paginationTransfer = $this->createPaginationTransfer($request);
360
        $criteriaFilterTransfer->setPagination($paginationTransfer);
361
362
        return $criteriaFilterTransfer;
363
    }
364
365
    /**
366
     * @param array $data
367
     *
368
     * @return \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer
369
     */
370
    protected function companyBusinessUnitSave(array $data): CompanyBusinessUnitResponseTransfer
371
    {
372
        $companyBusinessUnitTransfer = new CompanyBusinessUnitTransfer();
373
        $companyBusinessUnitTransfer->fromArray($data, true);
374
375
        $companyBusinessUnitClient = $this->getFactory()->getCompanyBusinessUnitClient();
376
377
        if ($companyBusinessUnitTransfer->getIdCompanyBusinessUnit()) {
378
            return $companyBusinessUnitClient->updateCompanyBusinessUnit($companyBusinessUnitTransfer);
379
        }
380
381
        return $companyBusinessUnitClient->createCompanyBusinessUnit($companyBusinessUnitTransfer);
382
    }
383
384
    /**
385
     * @param \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer
386
     *
387
     * @return void
388
     */
389
    protected function applyErrorMessage(CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer): void
390
    {
391
        foreach ($companyBusinessUnitResponseTransfer->getMessages() as $message) {
392
            $this->addTranslatedErrorMessage($message->getText(), [
393
                '%unit%' => $companyBusinessUnitResponseTransfer->getCompanyBusinessUnitTransfer()->getName(),
394
            ]);
395
        }
396
    }
397
398
    /**
399
     * @param \Generated\Shared\Transfer\CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer
400
     *
401
     * @return void
402
     */
403
    protected function applySuccessMessage(CompanyBusinessUnitResponseTransfer $companyBusinessUnitResponseTransfer): void
404
    {
405
        foreach ($companyBusinessUnitResponseTransfer->getMessages() as $message) {
406
            $this->addTranslatedSuccessMessage($message->getText(), [
407
                '%unit%' => $companyBusinessUnitResponseTransfer->getCompanyBusinessUnitTransfer()->getName(),
408
            ]);
409
        }
410
    }
411
}
412