Passed
Push — trunk ( 26fc38...0b2f4f )
by Christian
13:44 queued 13s
created

addPosition()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 39
nc 36
nop 2
dl 0
loc 59
rs 8.3626
c 0
b 0
f 0

How to fix   Long Method   

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 declare(strict_types=1);
2
3
namespace Shopware\Core\Test\Integration\Builder\Order;
4
5
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
6
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
7
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
8
use Shopware\Core\Checkout\Order\Aggregate\OrderTransactionCaptureRefund\OrderTransactionCaptureRefundStates;
9
use Shopware\Core\Content\Test\Product\ProductBuilder;
10
use Shopware\Core\Framework\Log\Package;
11
use Shopware\Core\Framework\Test\IdsCollection;
12
use Shopware\Core\Framework\Test\TestCaseBase\BasicTestDataBehaviour;
13
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
14
use Shopware\Core\Test\TestBuilderTrait;
15
16
/**
17
 * @final
18
 */
19
#[Package('customer-order')]
20
class OrderTransactionCaptureRefundBuilder
21
{
22
    use BasicTestDataBehaviour;
23
    use KernelTestBehaviour;
24
    use TestBuilderTrait;
25
26
    protected string $id;
27
28
    protected CalculatedPrice $amount;
29
30
    protected string $stateId;
31
32
    /**
33
     * @var array<string, array<string, mixed>>
34
     */
35
    protected array $positions = [];
36
37
    public function __construct(
38
        IdsCollection $ids,
39
        string $key,
40
        protected string $captureId,
41
        float $amount = 420.69,
42
        string $state = OrderTransactionCaptureRefundStates::STATE_OPEN,
43
        protected ?string $externalReference = null,
44
        protected ?string $reason = null
45
    ) {
46
        $this->id = $ids->get($key);
47
        $this->ids = $ids;
48
        $this->stateId = $this->getStateMachineState(
49
            OrderTransactionCaptureRefundStates::STATE_MACHINE,
50
            $state
51
        );
52
53
        $this->amount($amount);
54
    }
55
56
    public function amount(float $amount): self
57
    {
58
        $this->amount = new CalculatedPrice($amount, $amount, new CalculatedTaxCollection(), new TaxRuleCollection());
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param array<string, mixed> $customParams
65
     */
66
    public function addPosition(string $key, array $customParams = []): self
67
    {
68
        if (\array_key_exists('amount', $customParams)) {
69
            if (\is_float($customParams['amount'])) {
70
                $customParams['amount'] = new CalculatedPrice(
71
                    $customParams['amount'],
72
                    $customParams['amount'],
73
                    new CalculatedTaxCollection(),
74
                    new TaxRuleCollection()
75
                );
76
            }
77
        }
78
79
        $lineItem = [];
80
81
        // if orderLineItem is not submitted, create a orderLineItem on the fly
82
        if (!\array_key_exists('orderLineItem', $customParams)) {
83
            $lineItem = ['orderLineItem' => (new ProductBuilder($this->ids, '10000'))->build()];
84
            $lineItem['orderLineItem']['identifier'] = $this->ids->get('10000');
85
            $lineItem['orderLineItem']['quantity'] = 1;
86
            $lineItem['orderLineItem']['label'] = 'foo';
87
            $lineItem['orderLineItem']['price'] = new CalculatedPrice(
88
                420.69,
89
                420.69,
90
                new CalculatedTaxCollection(),
91
                new TaxRuleCollection()
92
            );
93
        }
94
95
        $orderLineItemId = null;
96
97
        if (\array_key_exists('orderLineItem', $lineItem)) {
98
            if (\array_key_exists('id', $lineItem['orderLineItem'])) {
99
                $orderLineItemId = $lineItem['orderLineItem']['id'];
100
            }
101
        }
102
103
        if (\array_key_exists('orderLineItem', $customParams)) {
104
            $orderLineItemId = $customParams['orderLineItem'];
105
        }
106
107
        $position = \array_replace([
108
            'refundId' => $this->id,
109
            'orderLineItemId' => $orderLineItemId,
110
            'quantity' => 1,
111
            'reason' => null,
112
            'refundPrice' => 420.69,
113
            'amount' => new CalculatedPrice(
114
                420.69,
115
                420.69,
116
                new CalculatedTaxCollection(),
117
                new TaxRuleCollection()
118
            ),
119
            'externalReference' => null,
120
        ], $customParams, $lineItem);
121
122
        $this->positions[$this->ids->get($key)] = $position;
123
124
        return $this;
125
    }
126
}
127