OrderService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 134
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A cancelOrder() 0 11 1
A getListing() 0 24 2
A addOrder() 0 6 1
A getTrackingCode() 0 5 1
A updateOrder() 0 18 1
A getOrder() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\EwarehousingConnector\Services;
14
15
use DateTime;
16
use Etrias\EwarehousingConnector\Client\EwarehousingClient;
17
use Etrias\EwarehousingConnector\Response\GetTrackingCodeResponse;
18
use Etrias\EwarehousingConnector\Response\OrderResponse;
19
use Etrias\EwarehousingConnector\Response\SuccessResponse;
20
use Etrias\EwarehousingConnector\Serializer\ServiceTrait;
21
use Etrias\EwarehousingConnector\Types\Address;
22
use Etrias\EwarehousingConnector\Types\Order;
23
use GuzzleHttp\RequestOptions;
24
use JMS\Serializer\SerializerInterface;
25
26
class OrderService implements OrderServiceInterface
27
{
28
    use ServiceTrait;
29
30
    /**
31
     * @var EwarehousingClient
32
     */
33
    protected $client;
34
    /**
35
     * @var SerializerInterface
36
     */
37
    protected $serializer;
38
39
    /**
40
     * OrderService constructor.
41
     *
42
     * @param EwarehousingClient  $client
43
     * @param SerializerInterface $serializer
44
     */
45
    public function __construct(EwarehousingClient $client, SerializerInterface $serializer)
46
    {
47
        $this->client = $client;
48
        $this->serializer = $serializer;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @return OrderResponse[]
55
     */
56
    public function getListing(
57
        DateTime $from,
58
        DateTime $to,
59
        $page = 1,
60
        $status = null,
61
        $sort = null,
62
        $direction = null,
63
        DateTime $updatedAfter = null
64
    ) {
65
        $data = [
66
            'from' => $from->format('Y-m-d'),
67
            'to' => $to->format('Y-m-d'),
68
            'page' => $page,
69
            'status' => $status,
70
            'sort' => $sort,
71
            'direction' => $direction,
72
        ];
73
        if ($updatedAfter) {
74
            $data['updated_after'] = $updatedAfter->format('Y-m-d H:i:s');
75
        }
76
77
        $guzzleResponse = $this->client->get('3/orders', [RequestOptions::QUERY => $data]);
78
79
        return $this->deserializeResponse($guzzleResponse, 'array<'.OrderResponse::class.'>');
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     *
85
     * @return OrderResponse
86
     */
87
    public function getOrder($reference)
88
    {
89
        $guzzleResponse = $this->client->get('2/orders/order', [RequestOptions::QUERY => ['reference' => $reference]]);
90
91
        return $this->deserializeResponse($guzzleResponse, OrderResponse::class);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @return SuccessResponse;
98
     */
99
    public function addOrder(Order $order)
100
    {
101
        $json = $this->serializer->serialize($order, 'json');
102
        $guzzleResponse = $this->client->post('/3/orders/create', [RequestOptions::FORM_PARAMS => json_decode($json, true)]);
103
104
        return $this->deserializeResponse($guzzleResponse, SuccessResponse::class);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @return SuccessResponse;
111
     */
112
    public function updateOrder(
113
        $reference,
114
        DateTime $date,
115
        Address $address = null,
116
        array $orderLines = null,
117
        $shippingMethod = null
118
    ) {
119
        $data = [
120
            'reference' => $reference,
121
            'date' => $date->format('Y-m-d'),
122
            'address' => json_decode($this->serializer->serialize($address, 'json'), true),
123
            'order_lines' => json_decode($this->serializer->serialize($orderLines, 'json'), true),
124
            'shipping_method' => $shippingMethod,
125
        ];
126
127
        $guzzleResponse = $this->client->post('/1/orders/update', [RequestOptions::FORM_PARAMS => $data]);
128
129
        return $this->deserializeResponse($guzzleResponse, SuccessResponse::class);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @return SuccessResponse;
136
     */
137
    public function cancelOrder(
138
        $reference,
139
        array $orderLines = []
140
    ) {
141
        $data = [
142
            'order_lines' => json_decode($this->serializer->serialize($orderLines, 'json'), true),
143
        ];
144
145
        $guzzleResponse = $this->client->post('/2/orders/cancel/'.$reference, [RequestOptions::FORM_PARAMS => $data]);
146
147
        return $this->deserializeResponse($guzzleResponse, SuccessResponse::class);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     *
153
     * @return GetTrackingCodeResponse[]
154
     */
155
    public function getTrackingCode(array $references)
156
    {
157
        $guzzleResponse = $this->client->get('5/orders/tracking', [RequestOptions::QUERY => ['reference' => $references]]);
158
159
        return $this->deserializeResponse($guzzleResponse, 'array<string, '.GetTrackingCodeResponse::class.'>');
160
    }
161
}
162