OrderDetails::payloadConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Getloy\TransactionDetails;
4
5
/**
6
 * Data structure for order details
7
 */
8
class OrderDetails
9
{
10
    protected $totalAmount;
11
    protected $currency;
12
    protected $orderTimestamp;
13
    protected $orderItems;
14
15
    /**
16
     * Instantiate order details.
17
     *
18
     * @param float $totalAmount The total order amount.
19
     * @param string $currency The order currency code.
20
     * @param \DateTime $orderTimestamp Order timestamp (optional, default is current date/time).
21
     * @param OrderItems $orderItems Order items collection (optional).
22
     */
23
    public function __construct(
24
        float $totalAmount,
25
        string $currency,
26
        \DateTime $orderTimestamp = null,
27
        OrderItems $orderItems = null
28
    ) {
29
30
        $this->totalAmount = $totalAmount;
31
        $this->currency = $currency;
32
        $this->orderTimestamp = $orderTimestamp ?: new \DateTime();
33
        $this->orderItems = $orderItems ?: new OrderItems();
34
    }
35
36
    /**
37
     * Get the total order amount
38
     *
39
     * @return float Total amount (in order currency)
40
     */
41
    public function totalAmount(): float
42
    {
43
        return $this->totalAmount;
44
    }
45
46
    /**
47
     * Get the order currency code
48
     *
49
     * @return string Order currency code
50
     */
51
    public function currency(): string
52
    {
53
        return $this->currency;
54
    }
55
56
    /**
57
     * Get the order items
58
     *
59
     * @return OrderItems Order items
60
     */
61
    public function orderItems(): OrderItems
62
    {
63
        return $this->orderItems;
64
    }
65
66
    /**
67
     * Generate partial GetLoy widget payload configuration for the instance.
68
     *
69
     * @return array Partial widget payload configuration.
70
     */
71
    public function payloadConfig(): array
72
    {
73
        return [
74
            'total_amount' => sprintf('%.2f', $this->totalAmount),
75
            'currency' => $this->currency,
76
            'order_timestamp' => date_format($this->orderTimestamp, DATE_ATOM),
77
            'order_items' => $this->orderItems->payloadConfig(),
78
        ];
79
    }
80
}
81