Failed Conditions
Push — master ( 36cd34...e25ade )
by
unknown
47:16 queued 16:37
created

ImportController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 41 4
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
declare(strict_types = 1);
9
10
namespace Spryker\Zed\DataImportMerchantPortalGui\Communication\Controller;
11
12
use Spryker\Zed\DataImportMerchantPortalGui\DataImportMerchantPortalGuiConfig;
13
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @method \Spryker\Zed\DataImportMerchantPortalGui\Communication\DataImportMerchantPortalGuiCommunicationFactory getFactory()
19
 */
20
class ImportController extends AbstractController
21
{
22
    /**
23
     * @var string
24
     */
25
    protected const TEMPLATE_FORM_START_IMPORT = 'DataImportMerchantPortalGui/Partials/start-import.twig';
26
27
    /**
28
     * @var string
29
     */
30
    protected const MESSAGE_INVALID_FORM_DATA = 'Invalid form data';
31
32
    /**
33
     * @param \Symfony\Component\HttpFoundation\Request $request
34
     *
35
     * @return \Symfony\Component\HttpFoundation\Response
36
     */
37
    public function indexAction(Request $request): Response
38
    {
39
        $dataImportMerchantFileForm = $this->getFactory()
40
            ->createDataImportMerchantFileForm()
41
            ->handleRequest($request);
42
43
        $formViewResponse = $this->renderView(
44
            static::TEMPLATE_FORM_START_IMPORT,
45
            [
46
                'form' => $dataImportMerchantFileForm->createView(),
47
                'maxFileSize' => DataImportMerchantPortalGuiConfig::MAX_FILE_SIZE_MB,
48
                'dataImportTemplates' => $this->getFactory()->getConfig()->getDataImportTemplates(),
49
            ],
50
        );
51
52
        if (!$dataImportMerchantFileForm->isSubmitted()) {
53
            return $this->jsonResponse(['form' => $formViewResponse->getContent()]);
54
        }
55
56
        $zedUiFormResponseBuilder = $this->getFactory()->getZedUiFactory()->createZedUiFormResponseBuilder();
57
58
        if (!$dataImportMerchantFileForm->isValid()) {
59
            if (!$dataImportMerchantFileForm->getErrors(true)->count()) {
60
                $zedUiFormResponseBuilder->addErrorNotification(
61
                    $this->getFactory()->getTranslatorFacade()->trans(static::MESSAGE_INVALID_FORM_DATA),
62
                );
63
            }
64
65
            return $this->jsonResponse(array_merge(
66
                ['form' => $formViewResponse->getContent()],
67
                $zedUiFormResponseBuilder->createResponse()->toArray(true, true),
68
            ));
69
        }
70
71
        $zedUiFormResponseBuilder = $this->getFactory()
72
            ->createDataImportMerchantFileHandler()
73
            ->handleDataImportMerchantFileCreation($dataImportMerchantFileForm, $zedUiFormResponseBuilder);
74
75
        return $this->jsonResponse(array_merge(
76
            ['form' => $formViewResponse->getContent()],
77
            $zedUiFormResponseBuilder->createResponse()->toArray(true, true),
78
        ));
79
    }
80
}
81