Completed
Pull Request — master (#52)
by
unknown
04:33 queued 01:58
created

Multiorders::populate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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