Passed
Push — master ( b14078...3674b6 )
by payever
03:28
created

OutwardActionProcessor::executeActionRequest()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 35
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 35
rs 6.6166
cc 13
nc 7
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
 * PHP version 5.4 and 7
4
 *
5
 * @package   Payever\ThirdParty
6
 * @author    Hennadii.Shymanskyi <[email protected]>
7
 * @copyright 2017-2019 payever GmbH
8
 * @license   MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Payever\ExternalIntegration\ThirdParty\Action;
12
13
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryChangedRequestEntity;
14
use Payever\ExternalIntegration\Inventory\Http\RequestEntity\InventoryCreateRequestEntity;
15
use Payever\ExternalIntegration\Inventory\InventoryApiClient;
16
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity;
17
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity;
18
use Payever\ExternalIntegration\Products\ProductsApiClient;
19
use Payever\ExternalIntegration\ThirdParty\Enum\ActionEnum;
20
use Psr\Log\LoggerInterface;
21
22
class OutwardActionProcessor
23
{
24
    /** @var ProductsApiClient */
25
    private $productsApiClient;
26
27
    /** @var InventoryApiClient */
28
    private $inventoryApiClient;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    public function __construct(
34
        ProductsApiClient $productsApiClient,
35
        InventoryApiClient $inventoryApiClient,
36
        LoggerInterface $logger
37
    ) {
38
        $this->productsApiClient = $productsApiClient;
39
        $this->inventoryApiClient = $inventoryApiClient;
40
        $this->logger = $logger;
41
    }
42
43
    /**
44
     * @param string $action - {@see ActionEnum}
45
     * @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload
46
     *
47
     * @throws \RuntimeException - when given action is unknown
48
     * @throws \Exception - bubbles up anything thrown by API
49
     */
50
    public function process($action, $payload)
51
    {
52
        $loggerPrefix = '[OUTWARD_ACTION_REQUEST]';
53
54
        $this->logger->info(
55
            sprintf('%s Processing action request', $loggerPrefix),
56
            compact('action')
57
        );
58
59
        $this->logger->debug(
60
            sprintf('%s Action request payload', $loggerPrefix),
61
            compact($payload)
62
        );
63
64
        try {
65
            $this->executeActionRequest($action, $payload);
66
        } catch (\Exception $exception) {
67
            $this->logger->critical(
68
                sprintf(
69
                    '%s Processing action failed. EXCEPTION: %s: %s',
70
                    $loggerPrefix,
71
                    $exception->getCode(),
72
                    $exception->getMessage()
73
                ),
74
                compact('action', 'payload')
75
            );
76
77
            throw $exception;
78
        }
79
    }
80
81
    /**
82
     * @param string $action - {@see ActionEnum}
83
     * @param InventoryChangedRequestEntity|ProductRequestEntity|ProductRemovedRequestEntity|array|string $payload
84
     *
85
     * @throws \Exception
86
     */
87
    private function executeActionRequest($action, $payload)
88
    {
89
        switch ($action) {
90
            case ActionEnum::ACTION_SET_INVENTORY:
91
                $this->inventoryApiClient->createInventory(
92
                    $payload instanceof InventoryCreateRequestEntity ? $payload : new InventoryCreateRequestEntity($payload)
0 ignored issues
show
introduced by
$payload is never a sub-type of Payever\ExternalIntegrat...toryCreateRequestEntity.
Loading history...
93
                );
94
                break;
95
            case ActionEnum::ACTION_ADD_INVENTORY:
96
                $this->inventoryApiClient->addInventory(
97
                    $payload instanceof InventoryChangedRequestEntity ? $payload : new InventoryChangedRequestEntity($payload)
98
                );
99
                break;
100
            case ActionEnum::ACTION_SUBTRACT_INVENTORY:
101
                $this->inventoryApiClient->subtractInventory(
102
                    $payload instanceof InventoryChangedRequestEntity ? $payload : new InventoryChangedRequestEntity($payload)
103
                );
104
                break;
105
            case ActionEnum::ACTION_CREATE_PRODUCT:
106
                $this->productsApiClient->createProduct(
107
                    $payload instanceof ProductRequestEntity ? $payload : new ProductRequestEntity($payload)
108
                );
109
                break;
110
            case ActionEnum::ACTION_UPDATE_PRODUCT:
111
                $this->productsApiClient->updateProduct(
112
                    $payload instanceof ProductRequestEntity ? $payload : new ProductRequestEntity($payload)
113
                );
114
                break;
115
            case ActionEnum::ACTION_REMOVE_PRODUCT:
116
                $this->productsApiClient->removeProduct(
117
                    $payload instanceof ProductRemovedRequestEntity ? $payload : new ProductRemovedRequestEntity($payload)
118
                );
119
                break;
120
            default:
121
                throw new \RuntimeException(sprintf('Unknown action %s', $action));
122
        }
123
    }
124
}
125