ConnectionController::handleResponseErrors()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
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\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
15
/**
16
 * @method \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface getRepository()
17
 * @method \SprykerEco\Zed\PunchoutCatalogs\Business\PunchoutCatalogsFacadeInterface getFacade()
18
 * @method \SprykerEco\Zed\PunchoutCatalogs\Communication\PunchoutCatalogsCommunicationFactory getFactory()
19
 */
20
class ConnectionController extends AbstractController
21
{
22
    protected const PARAM_ID_PUNCHOUT_CATALOG_CONNECTION = 'id-punchout-catalog-connection';
23
24
    /**
25
     * @see \SprykerEco\Zed\PunchoutCatalogs\Communication\Controller\IndexController::indexAction()
26
     */
27
    protected const ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE = '/punchout-catalogs';
28
29
    protected const MESSAGE_CONNECTION_NOT_FOUND = 'Connection not found';
30
    protected const MESSAGE_CONNECTION_ACTIVATED = 'Connection "%connection_name%" was activated.';
31
    protected const MESSAGE_CONNECTION_DEACTIVATED = 'Connection "%connection_name%" was deactivated.';
32
33
    protected const MESSAGE_PARAM_CONNECTION_NAME = '%connection_name%';
34
35
    /**
36
     * @param \Symfony\Component\HttpFoundation\Request $request
37
     *
38
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
39
     */
40
    public function activateAction(Request $request): RedirectResponse
41
    {
42
        $idPunchoutCatalogConnection = $this->castId(
43
            $request->query->get(static::PARAM_ID_PUNCHOUT_CATALOG_CONNECTION)
44
        );
45
46
        $punchoutCatalogConnectionTransfer = $this->getFacade()
47
            ->findConnectionById($idPunchoutCatalogConnection);
48
49
        if (!$punchoutCatalogConnectionTransfer) {
50
            $this->addErrorMessage(static::MESSAGE_CONNECTION_NOT_FOUND);
51
52
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
53
        }
54
55
        $punchoutCatalogConnectionTransfer->setIsActive(true);
56
        $punchoutCatalogResponseTransfer = $this->getFacade()
57
            ->updateConnection($punchoutCatalogConnectionTransfer);
58
59
        if ($punchoutCatalogResponseTransfer->getIsSuccessful()) {
60
            $this->addSuccessMessage(static::MESSAGE_CONNECTION_ACTIVATED, [
61
                static::MESSAGE_PARAM_CONNECTION_NAME => $punchoutCatalogConnectionTransfer->getName(),
62
            ]);
63
64
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
65
        }
66
67
        $this->handleResponseErrors($punchoutCatalogResponseTransfer);
68
69
        return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
70
    }
71
72
    /**
73
     * @param \Symfony\Component\HttpFoundation\Request $request
74
     *
75
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
76
     */
77
    public function deactivateAction(Request $request): RedirectResponse
78
    {
79
        $idPunchoutCatalogConnection = $this->castId(
80
            $request->query->get(static::PARAM_ID_PUNCHOUT_CATALOG_CONNECTION)
81
        );
82
83
        $punchoutCatalogConnectionTransfer = $this->getFacade()
84
            ->findConnectionById($idPunchoutCatalogConnection);
85
86
        if (!$punchoutCatalogConnectionTransfer) {
87
            $this->addErrorMessage(static::MESSAGE_CONNECTION_NOT_FOUND);
88
89
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
90
        }
91
92
        $punchoutCatalogConnectionTransfer->setIsActive(false);
93
        $punchoutCatalogResponseTransfer = $this->getFacade()
94
            ->updateConnection($punchoutCatalogConnectionTransfer);
95
96
        if ($punchoutCatalogResponseTransfer->getIsSuccessful()) {
97
            $this->addSuccessMessage(static::MESSAGE_CONNECTION_DEACTIVATED, [
98
                static::MESSAGE_PARAM_CONNECTION_NAME => $punchoutCatalogConnectionTransfer->getName(),
99
            ]);
100
101
            return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
102
        }
103
104
        $this->handleResponseErrors($punchoutCatalogResponseTransfer);
105
106
        return $this->redirectResponse(static::ROUTE_PUNCHOUT_CATALOGS_CONNECTION_LIST_PAGE);
107
    }
108
109
    /**
110
     * @param \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer $punchoutCatalogResponseTransfer
111
     *
112
     * @return void
113
     */
114
    protected function handleResponseErrors(PunchoutCatalogResponseTransfer $punchoutCatalogResponseTransfer): void
115
    {
116
        foreach ($punchoutCatalogResponseTransfer->getMessages() as $messageTransfer) {
117
            $this->addErrorMessage($messageTransfer->getValue());
118
        }
119
    }
120
}
121