OutwardActionProcessor::executeActionRequest()   C
last analyzed

Complexity

Conditions 13
Paths 14

Size

Total Lines 42
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
c 2
b 0
f 0
dl 0
loc 42
rs 6.6166
cc 13
nc 14
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Action
7
 * @package   Payever\ThirdParty
8
 * @author    payever GmbH <[email protected]>
9
 * @author    Hennadii.Shymanskyi <[email protected]>
10
 * @copyright 2017-2021 payever GmbH
11
 * @license   MIT <https://opensource.org/licenses/MIT>
12
 * @link      https://docs.payever.org/shopsystems/api/getting-started
13
 */
14
15
namespace Payever\ExternalIntegration\ThirdParty\Action;
16
17
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity;
18
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity;
19
use Payever\ExternalIntegration\Inventory\InventoryApiClient;
20
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity;
21
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity;
22
use Payever\ExternalIntegration\Products\ProductsApiClient;
23
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum;
24
use Psr\Log\LoggerInterface;
25
26
/**
27
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
28
 * @SuppressWarnings(PHPMD.MissingImport)
29
 */
30
class OutwardActionProcessor
31
{
32
    /** @var ProductsApiClient */
33
    private $productsApiClient;
34
35
    /** @var InventoryApiClient */
36
    private $inventoryApiClient;
37
38
    /** @var LoggerInterface */
39
    private $logger;
40
41
    /**
42
     * @param ProductsApiClient $productsApiClient
43
     * @param InventoryApiClient $inventoryApiClient
44
     * @param LoggerInterface $logger
45
     */
46
    public function __construct(
47
        ProductsApiClient $productsApiClient,
48
        InventoryApiClient $inventoryApiClient,
49
        LoggerInterface $logger
50
    ) {
51
        $this->productsApiClient = $productsApiClient;
52
        $this->inventoryApiClient = $inventoryApiClient;
53
        $this->logger = $logger;
54
    }
55
56
    /**
57
     * @param string $action - {@see ActionEnum}
58
     * @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload
59
     *
60
     * @throws \RuntimeException - when given action is unknown
61
     * @throws \Exception - bubbles up anything thrown by API
62
     */
63
    public function process($action, $payload)
64
    {
65
        $loggerPrefix = '[OUTWARD_ACTION_REQUEST]';
66
67
        $this->logger->info(
68
            sprintf('%s Processing action request', $loggerPrefix),
69
            compact('action')
70
        );
71
72
        $this->logger->debug(
73
            sprintf('%s Action request payload', $loggerPrefix),
74
            compact('action', 'payload')
75
        );
76
77
        try {
78
            $this->executeActionRequest($action, $payload);
79
        } catch (\Exception $exception) {
80
            $this->logger->critical(
81
                sprintf(
82
                    '%s Processing action failed. EXCEPTION: %s: %s',
83
                    $loggerPrefix,
84
                    $exception->getCode(),
85
                    $exception->getMessage()
86
                ),
87
                compact('action', 'payload')
88
            );
89
90
            throw $exception;
91
        }
92
    }
93
94
    /**
95
     * @param string $action - {@see ActionEnum}
96
     * @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload
97
     *
98
     * @throws \Exception
99
     */
100
    private function executeActionRequest($action, $payload)
101
    {
102
        if (is_string($payload)) {
103
            $payload = json_decode($payload, true);
104
        }
105
        switch ($action) {
106
            case ActionEnum::ACTION_SET_INVENTORY:
107
                $this->inventoryApiClient->createInventory(
108
                    $payload instanceof InventoryCreateRequestEntity
109
                        ? $payload
110
                        : new InventoryCreateRequestEntity($payload)
111
                );
112
                break;
113
            case ActionEnum::ACTION_ADD_INVENTORY:
114
                $this->inventoryApiClient->addInventory(
115
                    $payload instanceof InventoryChangedRequestEntity
116
                        ? $payload
117
                        : new InventoryChangedRequestEntity($payload)
118
                );
119
                break;
120
            case ActionEnum::ACTION_SUBTRACT_INVENTORY:
121
                $this->inventoryApiClient->subtractInventory(
122
                    $payload instanceof InventoryChangedRequestEntity
123
                        ? $payload
124
                        : new InventoryChangedRequestEntity($payload)
125
                );
126
                break;
127
            case ActionEnum::ACTION_CREATE_PRODUCT:
128
            case ActionEnum::ACTION_UPDATE_PRODUCT:
129
                $this->productsApiClient->createOrUpdateProduct(
130
                    $payload instanceof ProductRequestEntity ? $payload : new ProductRequestEntity($payload)
131
                );
132
                break;
133
            case ActionEnum::ACTION_REMOVE_PRODUCT:
134
                $this->productsApiClient->removeProduct(
135
                    $payload instanceof ProductRemovedRequestEntity
136
                        ? $payload
137
                        : new ProductRemovedRequestEntity($payload)
138
                );
139
                break;
140
            default:
141
                throw new \RuntimeException(sprintf('Unknown action %s', $action));
142
        }
143
    }
144
}
145