AbstractTriggerOmsEventCommandPlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 14
dl 0
loc 39
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 3
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types = 1);
9
10
namespace Pyz\Zed\MerchantOms\Communication\Plugin\Oms;
11
12
use Generated\Shared\Transfer\MerchantOrderItemCriteriaTransfer;
13
use Generated\Shared\Transfer\StateMachineItemTransfer;
14
use LogicException;
15
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
16
use Spryker\Zed\StateMachine\Dependency\Plugin\CommandPluginInterface;
17
18
/**
19
 * @method \Pyz\Zed\MerchantOms\Communication\MerchantOmsCommunicationFactory getFactory()
20
 * @method \Pyz\Zed\MerchantOms\MerchantOmsConfig getConfig()
21
 */
22
abstract class AbstractTriggerOmsEventCommandPlugin extends AbstractPlugin implements CommandPluginInterface
23
{
24
    /**
25
     * @return string
26
     */
27
    abstract public function getEventName(): string;
28
29
    /**
30
     * {@inheritDoc}
31
     * - Triggers event on a marketplace order item.
32
     *
33
     * @api
34
     *
35
     * @param \Generated\Shared\Transfer\StateMachineItemTransfer $stateMachineItemTransfer
36
     *
37
     * @throws \LogicException
38
     *
39
     * @return void
40
     */
41
    public function run(StateMachineItemTransfer $stateMachineItemTransfer): void
42
    {
43
        $merchantOrderItemTransfer = $this->getFactory()->getMerchantSalesOrderFacade()->findMerchantOrderItem(
44
            (new MerchantOrderItemCriteriaTransfer())
45
                ->setIdMerchantOrderItem($stateMachineItemTransfer->getIdentifier()),
46
        );
47
48
        if (!$merchantOrderItemTransfer) {
49
            return;
50
        }
51
52
        $result = $this->getFactory()
53
            ->getOmsFacade()
54
            ->triggerEventForOneOrderItem($this->getEventName(), $merchantOrderItemTransfer->getIdOrderItem());
55
56
        if ($result === null) {
57
            throw new LogicException(sprintf(
58
                'Sales Order Item #%s transition for event "%s" has not happened.',
59
                $merchantOrderItemTransfer->getIdOrderItem(),
60
                $this->getEventName(),
61
            ));
62
        }
63
    }
64
}
65