IndexController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 10
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 SprykerEco\Zed\PunchoutCatalogs\Communication\Controller;
9
10
use Generated\Shared\Transfer\PunchoutCatalogResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...CatalogResponseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Spryker\Zed\Kernel\Communication\Controller\AbstractController;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @method \SprykerEco\Zed\PunchoutCatalogs\Communication\PunchoutCatalogsCommunicationFactory getFactory()
18
 * @method \SprykerEco\Zed\PunchoutCatalogs\Business\PunchoutCatalogsFacadeInterface getFacade()
19
 * @method \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface getRepository()
20
 */
21
class IndexController extends AbstractController
22
{
23
    protected const PARAM_ID_PUNCHOUT_CATALOG_CONNECTION = 'id-punchout-catalog-connection';
24
25
    /**
26
     * @see SprykerEco\Zed\PunchoutCatalogs\Communication\Controller::indexAction()
27
     */
28
    protected const ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE = '/punchout-catalogs';
29
30
    protected const MESSAGE_CONNECTION_UPDATED = 'Connection "%connection_name%" was updated successfully.';
31
    protected const MESSAGE_CONNECTION_NOT_FOUND = 'Connection not found';
32
    protected const MESSAGE_CONNECTION_ADDED = 'Connection "%connection_name%" was created successfully.';
33
34
    protected const MESSAGE_PARAM_CONNECTION_NAME = '%connection_name%';
35
36
    /**
37
     * @return array
38
     */
39
    public function indexAction(): array
40
    {
41
        $table = $this->getFactory()
42
            ->createPunchoutCatalogsConnectionsTable();
43
44
        return $this->viewResponse([
45
            'punchoutCatalogsConnectionsTable' => $table->render(),
46
        ]);
47
    }
48
49
    /**
50
     * @return \Symfony\Component\HttpFoundation\JsonResponse
51
     */
52
    public function tableAction(): JsonResponse
53
    {
54
        $table = $this->getFactory()
55
            ->createPunchoutCatalogsConnectionsTable();
56
57
        return $this->jsonResponse($table->fetchData());
58
    }
59
60
    /**
61
     * @param \Symfony\Component\HttpFoundation\Request $request
62
     *
63
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
64
     */
65
    public function createAction(Request $request)
66
    {
67
        $punchoutCatalogConnectionsForm = $this->getFactory()
68
            ->getPunchoutCatalogConnectionForm();
69
70
        $punchoutCatalogConnectionsForm->handleRequest($request);
71
72
        if ($punchoutCatalogConnectionsForm->isSubmitted() && $punchoutCatalogConnectionsForm->isValid()) {
73
            $punchoutCatalogResponseTransfer = $this->getFacade()
74
                ->createConnection($punchoutCatalogConnectionsForm->getData());
75
76
            if ($punchoutCatalogResponseTransfer->getIsSuccessful()) {
77
                $this->addSuccessMessage(static::MESSAGE_CONNECTION_ADDED, [
78
                    static::MESSAGE_PARAM_CONNECTION_NAME => $punchoutCatalogResponseTransfer->getPunchoutCatalogConnection()
79
                        ->getName(),
80
                ]);
81
            }
82
83
            $this->handleResponseErrors($punchoutCatalogResponseTransfer);
84
85
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
86
        }
87
88
        return [
89
            'punchoutCatalogConnectionForm' => $punchoutCatalogConnectionsForm->createView(),
90
        ];
91
    }
92
93
    /**
94
     * @param \Symfony\Component\HttpFoundation\Request $request
95
     *
96
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
97
     */
98
    public function editAction(Request $request)
99
    {
100
        $idPunchoutCatalogConnection = $this->castId(
101
            $request->query->get(static::PARAM_ID_PUNCHOUT_CATALOG_CONNECTION)
102
        );
103
104
        $punchoutCatalogConnectionTransfer = $this->getFacade()
105
            ->findConnectionByIdWithPassword($idPunchoutCatalogConnection);
106
107
        if (!$punchoutCatalogConnectionTransfer) {
108
            $this->addErrorMessage(static::MESSAGE_CONNECTION_NOT_FOUND);
109
110
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
111
        }
112
113
        $punchoutCatalogConnectionEditForm = $this->getFactory()
114
            ->getPunchoutCatalogConnectionForm($punchoutCatalogConnectionTransfer);
115
116
        $punchoutCatalogConnectionEditForm->handleRequest($request);
117
118
        if ($punchoutCatalogConnectionEditForm->isSubmitted() && $punchoutCatalogConnectionEditForm->isValid()) {
119
            $this->processPunchoutCatalogConnectionEditForm($punchoutCatalogConnectionEditForm);
120
121
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
122
        }
123
124
        return [
125
            'punchoutCatalogConnectionForm' => $punchoutCatalogConnectionEditForm->createView(),
126
            'idPunchoutCatalogConnection' => $idPunchoutCatalogConnection,
127
        ];
128
    }
129
130
    /**
131
     * @param \Symfony\Component\Form\FormInterface $punchoutCatalogConnectionEditForm
132
     *
133
     * @return void
134
     */
135
    protected function processPunchoutCatalogConnectionEditForm(FormInterface $punchoutCatalogConnectionEditForm): void
136
    {
137
        $punchoutCatalogResponseTransfer = $this->getFacade()
138
            ->updateConnection($punchoutCatalogConnectionEditForm->getData());
139
140
        if ($punchoutCatalogResponseTransfer->getIsSuccessful()) {
141
            $this->addSuccessMessage(static::MESSAGE_CONNECTION_UPDATED, [
142
                static::MESSAGE_PARAM_CONNECTION_NAME => $punchoutCatalogResponseTransfer->getPunchoutCatalogConnection()
143
                    ->getName(),
144
            ]);
145
        }
146
147
        $this->handleResponseErrors($punchoutCatalogResponseTransfer);
148
    }
149
150
    /**
151
     * @param \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer $punchoutCatalogResponseTransfer
152
     *
153
     * @return void
154
     */
155
    protected function handleResponseErrors(PunchoutCatalogResponseTransfer $punchoutCatalogResponseTransfer): void
156
    {
157
        foreach ($punchoutCatalogResponseTransfer->getMessages() as $messageTransfer) {
158
            $this->addErrorMessage($messageTransfer->getValue());
159
        }
160
    }
161
}
162