ArticleDataTrait::appendArticleData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 3
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace Omnipay\BillPay\Message\RequestData;
4
5
use Omnipay\BillPay\Item;
6
use Omnipay\Common\Exception\InvalidRequestException;
7
use Omnipay\Common\ItemBag;
8
use SimpleXMLElement;
9
10
/**
11
 * Appends items data
12
 *
13
 * @author    Andreas Lange <[email protected]>
14
 * @copyright 2016, Connox GmbH
15
 * @license   MIT
16
 */
17
trait ArticleDataTrait
18
{
19
    /**
20
     * A list of items in this order
21
     *
22
     * @return ItemBag|null A bag containing items in this order
23
     *
24
     * @codeCoverageIgnore
25
     */
26
    abstract public function getItems();
27
28
    /**
29
     * @param SimpleXMLElement $data
30
     *
31
     * @throws InvalidRequestException
32
     */
33 11
    protected function appendArticleData(SimpleXMLElement $data)
34
    {
35 11
        $data->addChild('article_data');
36
37 11
        foreach ($this->getItems()->all() as $pos => $item) {
38 11
            if (!$item instanceof Item) {
39 1
                throw new InvalidRequestException('All items must be of instance of ' . Item::class);
40
            }
41
42
            /* @noinspection DisconnectedForeachInstructionInspection */
43 10
            $data->article_data[0]->addChild('article');
44 10
            $data->article_data[0]->article[$pos]['articleid'] = $item->getId();
45 10
            $data->article_data[0]->article[$pos]['articlequantity'] = $item->getQuantity();
46 10
            $data->article_data[0]->article[$pos]['articlename'] = $item->getName();
47 10
            $data->article_data[0]->article[$pos]['articledescription'] = $item->getDescription();
48 10
            $data->article_data[0]->article[$pos]['articleprice'] = bcmul($item->getPriceNet(), 100, 0);
49 10
            $data->article_data[0]->article[$pos]['articlepricegross'] = bcmul($item->getPrice(), 100, 0);
50 10
        }
51 10
    }
52
}
53