Multiorders::populate()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 1
1
<?php
2
3
namespace Moip\Resource;
4
5
use ArrayIterator;
6
use stdClass;
7
8
/**
9
 * Class Multiorders.
10
 */
11
class Multiorders extends MoipResource
12
{
13
    /**
14
     * @const string
15
     */
16
    const PATH = 'multiorders';
17
18
    /**
19
     * Initializes new instances.
20
     */
21
    public function initialize()
22
    {
23
        $this->data = new stdClass();
24
        $this->data->ownId = null;
25
        $this->data->orders = [];
26
    }
27
28
    /**
29
     * Structure of order.
30
     *
31
     * @param Orders $order
32
     *
33
     * @return $this
34
     */
35
    public function addOrder(Orders $order)
36
    {
37
        $this->data->orders[] = $order;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Create a new multiorder in MoIP.
44
     *
45
     * @return stdClass
46
     */
47
    public function create()
48
    {
49
        return $this->createResource(sprintf('/%s/%s', MoipResource::VERSION, self::PATH));
50
    }
51
52
    /**
53
     * Get an multiorder in MoIP.
54
     *
55
     * @param string $id_moip Id MoIP order id
56
     *
57
     * @return stdClass
58
     */
59
    public function get($id_moip)
60
    {
61
        return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip));
62
    }
63
64
    /**
65
     * Get MoIP order id.
66
     *
67
     * @return string
68
     */
69
    public function getId()
70
    {
71
        return $this->getIfSet('id');
72
    }
73
74
    /**
75
     * Get own request id. external reference.
76
     *
77
     * @return string
78
     */
79
    public function getOwnId()
80
    {
81
        return $this->getIfSet('ownId');
82
    }
83
84
    /**
85
     * Get order status.
86
     * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED.
87
     *
88
     * @return string
89
     */
90
    public function getStatus()
91
    {
92
        return $this->getIfSet('status');
93
    }
94
95
    /**
96
     * Get total value of order.
97
     *
98
     * @return int|float
99
     */
100
    public function getAmountTotal()
101
    {
102
        return $this->getIfSet('total', $this->data->amount);
103
    }
104
105
    /**
106
     * Get currency used in the application. Possible values: BRL.
107
     *
108
     * @return string
109
     */
110
    public function getAmountCurrency()
111
    {
112
        return $this->getIfSet('currency', $this->data->amount);
113
    }
114
115
    /**
116
     * Get creation date of launch.
117
     *
118
     * @return \DateTime
119
     */
120
    public function getCreatedAt()
121
    {
122
        // todo: didn't find createdAt type on documentation, assuming datetime. Have to confirm
123
        return $this->getIfSetDateTime('createdAt');
124
    }
125
126
    /**
127
     * Get date of last update.
128
     *
129
     * @return \DateTime
130
     */
131
    public function getUpdatedAt()
132
    {
133
        return $this->getIfSetDateTime('updatedAt');
134
    }
135
136
    /**
137
     * Get orders.
138
     *
139
     * @return \ArrayIterator
140
     */
141
    public function getOrderIterator()
142
    {
143
        return new ArrayIterator($this->getIfSet('orders'));
144
    }
145
146
    /**
147
     * Structure of multipayments.
148
     *
149
     * @return \Moip\Resource\Payment
150
     */
151
    public function multipayments()
152
    {
153
        $payments = new Payment($this->moip);
154
        $payments->setMultiorder($this);
155
156
        return $payments;
157
    }
158
159
    /**
160
     * Mount the structure of order.
161
     *
162
     * @param \stdClass $response
163
     *
164
     * @return Multiorders
165
     */
166
    protected function populate(stdClass $response)
167
    {
168
        $multiorders = clone $this;
169
170
        $multiorders->data->id = $response->id;
171
        $multiorders->data->ownId = $response->ownId;
172
        $multiorders->data->status = $response->status;
173
        $multiorders->data->amount = new stdClass();
174
        $multiorders->data->amount->total = $response->amount->total;
175
        $multiorders->data->amount->currency = $response->amount->currency;
176
        $multiorders->data->orders = [];
177
178
        foreach ($response->orders as $responseOrder) {
179
            $order = new Orders($multiorders->moip);
180
            $order->populate($responseOrder);
181
182
            $multiorders->data->orders[] = $order;
183
        }
184
185
        $multiorders->data->createdAt = $response->createdAt;
186
        $multiorders->data->updatedAt = $response->updatedAt;
187
        $multiorders->data->_links = $response->_links;
188
189
        return $multiorders;
190
    }
191
192
    /**
193
     * Set own request id. External reference.
194
     *
195
     * @param string $ownId
196
     *
197
     * @return $this
198
     */
199
    public function setOwnId($ownId)
200
    {
201
        $this->data->ownId = $ownId;
202
203
        return $this;
204
    }
205
}
206