OrderItem   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A payloadConfig() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace Getloy\TransactionDetails;
4
5
/**
6
 * Single transaction order item.
7
 */
8
class OrderItem
9
{
10
    protected $description;
11
    protected $quantity;
12
    protected $totalPrice;
13
    protected $unitPrice;
14
15
    /**
16
     * Instantiate order item.
17
     *
18
     * @param string $description Order item description.
19
     * @param integer $quantity Ordered quantity.
20
     * @param float $totalPrice Total price for the ordered items.
21
     * @param float $unitPrice Price per unit (optional).
22
     */
23
    public function __construct(
24
        string $description,
25
        int $quantity,
26
        float $totalPrice,
27
        float $unitPrice = null
28
    ) {
29
30
        $this->description = $description;
31
        $this->quantity = $quantity;
32
        $this->totalPrice = round($totalPrice, 2);
33
        $this->unitPrice = round($unitPrice, 2);
34
    }
35
36
    /**
37
     * Generate partial GetLoy widget payload configuration for the instance.
38
     *
39
     * @return array Partial widget payload configuration.
40
     */
41
    public function payloadConfig(): array
42
    {
43
        $payload = [
44
            'description' => $this->description,
45
            'quantity' => $this->quantity,
46
            'total_price' => $this->totalPrice,
47
        ];
48
        if ($this->unitPrice) {
49
            $payload['unit_price'] = $this->unitPrice;
50
        }
51
        return $payload;
52
    }
53
}
54