Completed
Push — master ( 35e829...731b5b )
by Marco
01:15
created

CreateBasketRequest::getData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
4
namespace Omnipay\Heidelpay\Message;
5
6
use Omnipay\Common\Item;
7
8
class CreateBasketRequest extends AbstractRequest
9
{
10
    /**
11
     * @return array
12
     * @throws \Omnipay\Common\Exception\InvalidRequestException
13
     */
14
    public function getData()
15
    {
16
        $this->validate('transactionId', 'currency', 'items', 'amount');
17
18
        $data = [
19
            'amountTotalGross' => $this->getAmountInteger() / 100,
20
            'currencyCode' => $this->getCurrency(),
21
            'orderId' => $this->getTransactionId(),
22
            'note' => $this->getDescription(),
23
            'basketItems' => $this->getItemsData()
24
        ];
25
26
        if ($this->getTaxAmount()) {
27
            $data['amountTotalVat'] = $this->getTaxAmount();
28
        }
29
30
        if ($this->getDiscount()) {
31
            $data['amountTotalDiscount'] = $this->getDiscount();
32
        }
33
34
        return $data;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getItemsData()
41
    {
42
        $data = [];
43
44
        /** @var Item $item */
45
        foreach ($this->getItems() as $item) {
0 ignored issues
show
Bug introduced by
The expression $this->getItems() of type object<Omnipay\Common\ItemBag>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            $data[] = [
47
                'basketItemReferenceId' => uniqid(),
48
                'quantity' => $item->getQuantity(),
49
                'amountPerUnit' => $item->getPrice(),
50
                'amountGross' => $item->getPrice() * $item->getQuantity(),
51
                'title' => $item->getName(),
52
                'subTitle' => $item->getDescription()
53
            ];
54
        }
55
56
        return $data;
57
    }
58
59
60
    /**
61
     * @return string
62
     */
63
    public function getTaxAmount()
64
    {
65
        return $this->getParameter('tax_amount');
66
    }
67
68
    /**
69
     * @param $value
70
     * @return CreateBasketRequest
71
     */
72
    public function setTaxAmount($value)
73
    {
74
        return $this->setParameter('tax_amount', $value);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getDiscount()
81
    {
82
        return $this->getParameter('discount');
83
    }
84
85
    /**
86
     * @param $value
87
     * @return CreateBasketRequest
88
     */
89
    public function setDiscount($value)
90
    {
91
        return $this->setParameter('discount', $value);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getEndpoint()
98
    {
99
        return parent::getEndpoint().'baskets';
100
    }
101
}
102