Issues (3641)

Business/Deleter/MerchantRelationshipDeleter.php (1 issue)

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 Spryker\Zed\MerchantRelationship\Business\Deleter;
9
10
use Generated\Shared\Transfer\MerchantRelationshipConditionsTransfer;
11
use Generated\Shared\Transfer\MerchantRelationshipCriteriaTransfer;
12
use Generated\Shared\Transfer\MerchantRelationshipRequestTransfer;
13
use Generated\Shared\Transfer\MerchantRelationshipTransfer;
14
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
15
use Spryker\Zed\MerchantRelationship\Business\Model\MerchantRelationshipReaderInterface;
16
use Spryker\Zed\MerchantRelationship\Persistence\MerchantRelationshipEntityManagerInterface;
17
18
class MerchantRelationshipDeleter implements MerchantRelationshipDeleterInterface
19
{
20
    use TransactionTrait;
21
22
    /**
23
     * @var \Spryker\Zed\MerchantRelationship\Persistence\MerchantRelationshipEntityManagerInterface
24
     */
25
    protected $merchantRelationshipEntityManager;
26
27
    /**
28
     * @var \Spryker\Zed\MerchantRelationship\Business\Model\MerchantRelationshipReaderInterface
29
     */
30
    protected MerchantRelationshipReaderInterface $merchantRelationshipReader;
31
32
    /**
33
     * @var list<\Spryker\Zed\MerchantRelationshipExtension\Dependency\Plugin\MerchantRelationshipPreDeletePluginInterface>
34
     */
35
    protected $merchantRelationshipPreDeletePlugins;
36
37
    /**
38
     * @var list<\Spryker\Zed\MerchantRelationshipExtension\Dependency\Plugin\MerchantRelationshipPostDeletePluginInterface>
39
     */
40
    protected array $merchantRelationshipPostDeletePlugins;
41
42
    /**
43
     * @param \Spryker\Zed\MerchantRelationship\Persistence\MerchantRelationshipEntityManagerInterface $merchantRelationshipEntityManager
44
     * @param \Spryker\Zed\MerchantRelationship\Business\Model\MerchantRelationshipReaderInterface $merchantRelationshipReader
45
     * @param list<\Spryker\Zed\MerchantRelationshipExtension\Dependency\Plugin\MerchantRelationshipPreDeletePluginInterface> $merchantRelationshipPreDeletePlugins
46
     * @param list<\Spryker\Zed\MerchantRelationshipExtension\Dependency\Plugin\MerchantRelationshipPostDeletePluginInterface> $merchantRelationshipPostDeletePlugins
47
     */
48
    public function __construct(
49
        MerchantRelationshipEntityManagerInterface $merchantRelationshipEntityManager,
50
        MerchantRelationshipReaderInterface $merchantRelationshipReader,
51
        array $merchantRelationshipPreDeletePlugins,
52
        array $merchantRelationshipPostDeletePlugins
53
    ) {
54
        $this->merchantRelationshipEntityManager = $merchantRelationshipEntityManager;
55
        $this->merchantRelationshipReader = $merchantRelationshipReader;
56
        $this->merchantRelationshipPreDeletePlugins = $merchantRelationshipPreDeletePlugins;
0 ignored issues
show
Documentation Bug introduced by
It seems like $merchantRelationshipPreDeletePlugins of type array is incompatible with the declared type Spryker\Zed\MerchantRela...p\Business\Deleter\list of property $merchantRelationshipPreDeletePlugins.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->merchantRelationshipPostDeletePlugins = $merchantRelationshipPostDeletePlugins;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
62
     * @param \Generated\Shared\Transfer\MerchantRelationshipRequestTransfer|null $merchantRelationshipRequestTransfer
63
     *
64
     * @return void
65
     */
66
    public function delete(
67
        MerchantRelationshipTransfer $merchantRelationshipTransfer,
68
        ?MerchantRelationshipRequestTransfer $merchantRelationshipRequestTransfer = null
69
    ): void {
70
        if ($merchantRelationshipRequestTransfer === null) {
71
            trigger_error(
72
                '[Spryker/MerchantRelationship] Pass the $merchantRelationshipRequestTransfer parameter '
73
                . 'and use only it in this method for the forward compatibility with next major version.',
74
                E_USER_DEPRECATED,
75
            );
76
        }
77
78
        if ($merchantRelationshipRequestTransfer && $merchantRelationshipRequestTransfer->getMerchantRelationshipOrFail()->getIdMerchantRelationship()) {
79
            $merchantRelationshipTransfer = $merchantRelationshipRequestTransfer->getMerchantRelationshipOrFail();
80
        }
81
82
        $merchantRelationshipTransfer = $this->findMerchantRelationship($merchantRelationshipTransfer);
83
        if (!$merchantRelationshipTransfer) {
84
            return;
85
        }
86
87
        $this->deleteByMerchantRelationshipTransfer($merchantRelationshipTransfer);
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
92
     *
93
     * @return void
94
     */
95
    protected function deleteByMerchantRelationshipTransfer(MerchantRelationshipTransfer $merchantRelationshipTransfer): void
96
    {
97
        $this->getTransactionHandler()->handleTransaction(function () use ($merchantRelationshipTransfer) {
98
            $this->executeDeleteMerchantRelationshipByIdTransaction($merchantRelationshipTransfer);
99
        });
100
    }
101
102
    /**
103
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
104
     *
105
     * @return void
106
     */
107
    protected function executeDeleteMerchantRelationshipByIdTransaction(MerchantRelationshipTransfer $merchantRelationshipTransfer): void
108
    {
109
        $this->executeMerchantRelationshipPreDeletePlugins($merchantRelationshipTransfer);
110
        $this->merchantRelationshipEntityManager->deleteMerchantRelationshipById($merchantRelationshipTransfer->getIdMerchantRelationship());
111
        $this->executeMerchantRelationshipPostDeletePlugins($merchantRelationshipTransfer);
112
    }
113
114
    /**
115
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
116
     *
117
     * @return void
118
     */
119
    protected function executeMerchantRelationshipPreDeletePlugins(MerchantRelationshipTransfer $merchantRelationshipTransfer): void
120
    {
121
        foreach ($this->merchantRelationshipPreDeletePlugins as $merchantRelationshipPreDeletePlugin) {
122
            $merchantRelationshipPreDeletePlugin->execute($merchantRelationshipTransfer);
123
        }
124
    }
125
126
    /**
127
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
128
     *
129
     * @return void
130
     */
131
    protected function executeMerchantRelationshipPostDeletePlugins(MerchantRelationshipTransfer $merchantRelationshipTransfer): void
132
    {
133
        foreach ($this->merchantRelationshipPostDeletePlugins as $merchantRelationshipPostDeletePlugin) {
134
            $merchantRelationshipPostDeletePlugin->execute($merchantRelationshipTransfer);
135
        }
136
    }
137
138
    /**
139
     * @param \Generated\Shared\Transfer\MerchantRelationshipTransfer $merchantRelationshipTransfer
140
     *
141
     * @return \Generated\Shared\Transfer\MerchantRelationshipTransfer|null
142
     */
143
    protected function findMerchantRelationship(MerchantRelationshipTransfer $merchantRelationshipTransfer): ?MerchantRelationshipTransfer
144
    {
145
        $merchantRelationshipConditionsTransfer = (new MerchantRelationshipConditionsTransfer())
146
            ->addIdMerchantRelationship($merchantRelationshipTransfer->getIdMerchantRelationshipOrFail());
147
        $merchantRelationshipCriteriaTransfer = (new MerchantRelationshipCriteriaTransfer())
148
            ->setMerchantRelationshipConditions($merchantRelationshipConditionsTransfer);
149
150
        /** @var \Generated\Shared\Transfer\MerchantRelationshipCollectionTransfer $merchantRelationshipCollectionTransfer */
151
        $merchantRelationshipCollectionTransfer = $this->merchantRelationshipReader->getMerchantRelationshipCollection(
152
            null,
153
            $merchantRelationshipCriteriaTransfer,
154
        );
155
156
        return $merchantRelationshipCollectionTransfer->getMerchantRelationships()
157
            ->getIterator()
158
            ->current();
159
    }
160
}
161