Passed
Push — master ( 6c42be...776013 )
by Igor
02:03
created

ActionPayload::getPayloadEntity()   B

Complexity

Conditions 11
Paths 58

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 32
rs 7.3166
c 0
b 0
f 0
cc 11
nc 58
nop 0

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\Core\Base\MessageEntity;
14
use Payever\ExternalIntegration\Inventory\Http\MessageEntity\InventoryChangedEntity;
15
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRemovedRequestEntity;
16
use Payever\ExternalIntegration\Products\Http\RequestEntity\ProductRequestEntity;
17
use Payever\ExternalIntegration\ThirdParty\Enum\Action;
18
19
/**
20
 * PHP version 5.4 and 7
21
 *
22
 * @package   Payever\ThirdParty
23
 * @author    Hennadii.Shymanskyi <[email protected]>
24
 * @copyright 2017-2019 payever GmbH
25
 * @license   MIT <https://opensource.org/licenses/MIT>
26
 */
27
class ActionPayload
28
{
29
    /** @var string */
30
    protected $action;
31
32
    /** @var string */
33
    protected $rawPayload;
34
35
    /**
36
     * ActionPayload constructor.
37
     *
38
     * @param string $action @see Action
39
     * @param string|null $rawPayload
40
     */
41
    public function __construct($action, $rawPayload = null)
42
    {
43
        $this->action = $action;
44
        $this->rawPayload = $rawPayload;
45
    }
46
47
    /**
48
     * @return MessageEntity
49
     *
50
     * @throws \UnexpectedValueException when can't fetch request payload
51
     * @throws \RuntimeException when can't map action to payload entity
52
     */
53
    public function getPayloadEntity()
54
    {
55
        if (!$this->rawPayload) {
56
            $this->rawPayload = $this->getRequestPayload();
57
        }
58
59
        if (!$this->rawPayload) {
60
            throw new \UnexpectedValueException('Got empty action request payload.', 40);
61
        }
62
63
        if (is_string($this->rawPayload)) {
0 ignored issues
show
introduced by
The condition is_string($this->rawPayload) is always true.
Loading history...
64
            $payload = $this->unserializePayload($this->rawPayload);
65
        }
66
67
        if (isset($payload['payload'])) {
68
            $payload = $payload['payload'];
69
        }
70
71
        switch ($this->action) {
72
            case Action::ACTION_CREATE_PRODUCT:
73
            case Action::ACTION_UPDATE_PRODUCT:
74
                return new ProductRequestEntity($payload);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $payload does not seem to be defined for all execution paths leading up to this point.
Loading history...
75
            case Action::ACTION_REMOVE_PRODUCT:
76
                return new ProductRemovedRequestEntity($payload);
77
            case Action::ACTION_ADD_INVENTORY:
78
            case Action::ACTION_SET_INVENTORY:
79
            case Action::ACTION_SUBTRACT_INVENTORY:
80
                return new InventoryChangedEntity($payload);
81
            default:
82
                throw new \RuntimeException(
83
                    sprintf('Payload entity not found for action %s', $this->action),
84
                    41
85
                );
86
        }
87
    }
88
89
    /**
90
     * @return false|string
91
     */
92
    public function getRawPayload()
93
    {
94
        return $this->rawPayload ?: $this->getRequestPayload();
95
    }
96
97
    /**
98
     * @param string $payload
99
     * @return array
100
     */
101
    protected function unserializePayload($payload)
102
    {
103
        return json_decode($payload, true);
104
    }
105
106
    /**
107
     * @return false|string
108
     */
109
    protected function getRequestPayload()
110
    {
111
        return file_get_contents('php://input');
112
    }
113
}
114