PunchoutCatalogsWriter::storePassword()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
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\Business\Writer;
9
10
use Generated\Shared\Transfer\MessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\MessageTransfer 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 Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...talogConnectionTransfer 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...
12
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...
13
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
14
use SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToVaultFacadeInterface;
15
use SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsEntityManagerInterface;
16
use SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface;
17
18
class PunchoutCatalogsWriter implements PunchoutCatalogsWriterInterface
19
{
20
    use TransactionTrait;
21
22
    protected const MESSAGE_ERROR_DURING_CONNECTION_UPDATE = 'Error during connection update';
23
    protected const PASSWORD_VAULT_DATA_TYPE = 'pwg_punchout_catalog_connection.password';
24
25
    /**
26
     * @var \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsEntityManagerInterface
27
     */
28
    protected $punchoutCatalogsEntityManager;
29
30
    /**
31
     * @var \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface
32
     */
33
    protected $punchoutCatalogsRepository;
34
35
    /**
36
     * @var \SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToVaultFacadeInterface
37
     */
38
    protected $vaultFacade;
39
40
    /**
41
     * @param \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsEntityManagerInterface $punchoutCatalogsEntityManager
42
     * @param \SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToVaultFacadeInterface $vaultFacade
43
     * @param \SprykerEco\Zed\PunchoutCatalogs\Persistence\PunchoutCatalogsRepositoryInterface $punchoutCatalogsRepository
44
     */
45
    public function __construct(
46
        PunchoutCatalogsEntityManagerInterface $punchoutCatalogsEntityManager,
47
        PunchoutCatalogsToVaultFacadeInterface $vaultFacade,
48
        PunchoutCatalogsRepositoryInterface $punchoutCatalogsRepository
49
    ) {
50
        $this->punchoutCatalogsEntityManager = $punchoutCatalogsEntityManager;
51
        $this->vaultFacade = $vaultFacade;
52
        $this->punchoutCatalogsRepository = $punchoutCatalogsRepository;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer
57
     *
58
     * @return \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer
59
     */
60
    public function createConnection(PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer): PunchoutCatalogResponseTransfer
61
    {
62
        return $this->getTransactionHandler()->handleTransaction(function () use ($punchoutCatalogConnectionTransfer) {
63
            return $this->executeCreateConnectionTransaction($punchoutCatalogConnectionTransfer);
64
        });
65
    }
66
67
    /**
68
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer
69
     *
70
     * @return \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer
71
     */
72
    protected function executeCreateConnectionTransaction(PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer): PunchoutCatalogResponseTransfer
73
    {
74
        $punchoutCatalogConnectionTransfer = $this->punchoutCatalogsEntityManager->createPunchoutCatalogConnection($punchoutCatalogConnectionTransfer);
75
        $punchoutCatalogConnectionTransfer->setSetup(
76
            $this->punchoutCatalogsEntityManager->createPunchoutCatalogConnectionSetup(
77
                $punchoutCatalogConnectionTransfer->getIdPunchoutCatalogConnection(),
78
                $punchoutCatalogConnectionTransfer->getSetup()
79
            )
80
        );
81
        $punchoutCatalogConnectionTransfer->setCart(
82
            $this->punchoutCatalogsEntityManager->createPunchoutCatalogConnectionCart(
83
                $punchoutCatalogConnectionTransfer->getIdPunchoutCatalogConnection(),
84
                $punchoutCatalogConnectionTransfer->getCart()
85
            )
86
        );
87
88
        $this->storePassword($punchoutCatalogConnectionTransfer);
89
90
        return (new PunchoutCatalogResponseTransfer())
91
            ->setPunchoutCatalogConnection($punchoutCatalogConnectionTransfer)
92
            ->setIsSuccessful(true);
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer
97
     *
98
     * @return \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer
99
     */
100
    public function updateConnection(PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer): PunchoutCatalogResponseTransfer
101
    {
102
        return $this->getTransactionHandler()->handleTransaction(function () use ($punchoutCatalogConnectionTransfer) {
103
            return $this->executeUpdateConnectionTransaction($punchoutCatalogConnectionTransfer);
104
        });
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer
109
     *
110
     * @return \Generated\Shared\Transfer\PunchoutCatalogResponseTransfer
111
     */
112
    protected function executeUpdateConnectionTransaction(PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer): PunchoutCatalogResponseTransfer
113
    {
114
        $punchoutCatalogConnectionTransfer->requireIdPunchoutCatalogConnection();
115
        $existingPunchoutCatalogConnectionTransfer = $this->punchoutCatalogsRepository->findConnectionById(
116
            $punchoutCatalogConnectionTransfer->getIdPunchoutCatalogConnection()
117
        );
118
119
        if (!$existingPunchoutCatalogConnectionTransfer) {
120
            return (new PunchoutCatalogResponseTransfer())
121
                ->addMessage((new MessageTransfer())->setValue(static::MESSAGE_ERROR_DURING_CONNECTION_UPDATE))
122
                ->setIsSuccessful(false);
123
        }
124
125
        $this->punchoutCatalogsEntityManager->updatePunchoutCatalogConnection($punchoutCatalogConnectionTransfer);
126
        $this->punchoutCatalogsEntityManager->updatePunchoutCatalogConnectionSetup($punchoutCatalogConnectionTransfer->getSetup());
127
        $this->punchoutCatalogsEntityManager->updatePunchoutCatalogConnectionCart($punchoutCatalogConnectionTransfer->getCart());
128
129
        $this->storePassword($punchoutCatalogConnectionTransfer);
130
131
        return (new PunchoutCatalogResponseTransfer())
132
            ->setPunchoutCatalogConnection($punchoutCatalogConnectionTransfer)
133
            ->setIsSuccessful(true);
134
    }
135
136
    /**
137
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer
138
     *
139
     * @return void
140
     */
141
    protected function storePassword(PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer): void
142
    {
143
        if (!$punchoutCatalogConnectionTransfer->isPropertyModified(PunchoutCatalogConnectionTransfer::PASSWORD)) {
144
            return;
145
        }
146
147
        $this->vaultFacade->store(
148
            static::PASSWORD_VAULT_DATA_TYPE,
149
            (string)$punchoutCatalogConnectionTransfer->getIdPunchoutCatalogConnection(),
150
            $punchoutCatalogConnectionTransfer->getPassword()
151
        );
152
    }
153
}
154