Completed
Push — master ( fb89e6...384431 )
by Christian
08:38 queued 11s
created

MWSClient   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 5
dl 0
loc 237
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setMarketPlaces() 0 5 2
A getCurrentMarketPlaces() 0 4 1
A getTimeStamp() 0 4 1
A getAccessKeyId() 0 4 1
A setAccessKeyId() 0 6 1
A getSellerId() 0 4 1
A setSellerId() 0 6 1
A getSignatureMethod() 0 4 1
A getSignatureVersion() 0 4 1
A getDomain() 0 10 2
A getCountry() 0 4 1
A setCountry() 0 10 2
A getMarketPlaceIds() 0 4 1
A post() 0 24 2
A getDefaultQueryParams() 0 17 1
A getMarketPlaceParams() 0 10 2
A generateRequestUri() 0 4 1
A getQueryStringForSignature() 0 10 1
A generateSignature() 0 13 1
A getQuery() 0 7 1
1
<?php
2
3
namespace Looxis\LaravelAmazonMWS;
4
5
use GuzzleHttp\Client;
6
use Looxis\LaravelAmazonMWS\Exceptions\CountryIsMissingException;
7
use Looxis\LaravelAmazonMWS\Exceptions\CountryNotAvailableException;
8
9
class MWSClient
10
{
11
    const SIGNATURE_METHOD = 'HmacSHA256';
12
    const SIGNATURE_VERSION = '2';
13
    const DATE_FORMAT = "Y-m-d\TH:i:s.\\0\\0\\0\\Z";
14
    const APPLICATION_NAME = 'Looxis/MwsClient';
15
    const APPLICATION_VERSION = '0.0.1';
16
17
    protected $accessKeyId;
18
    protected $secretKey;
19
    protected $sellerId;
20
    protected $client;
21
    protected $marketPlaces;
22
23
    protected $marketplaceIds = [
24
        'A2Q3Y263D00KWC' => 'mws.amazonservices.com',
25
        'A2EUQ1WTGCTBG2' => 'mws.amazonservices.ca',
26
        'A1AM78C64UM0Y8' => 'mws.amazonservices.com.mx',
27
        'ATVPDKIKX0DER' => 'mws.amazonservices.com',
28
        'A2VIGQ35RCS4UG' => 'mws.amazonservices.ae',
29
        'A1PA6795UKMFR9' => 'mws-eu.amazonservices.com',
30
        'ARBP9OOSHTCHU' => 'mws-eu.amazonservices.com',
31
        'A1RKKUPIHCS9HS' => 'mws-eu.amazonservices.com',
32
        'A13V1IB3VIYZZH' => 'mws-eu.amazonservices.com',
33
        'A1F83G8C2ARO7P' => 'mws-eu.amazonservices.com',
34
        'A21TJRUUN4KGV' => 'mws.amazonservices.in',
35
        'APJ6JRA9NG5V4' => 'mws-eu.amazonservices.com',
36
        'A17E79C6D8DWNP' => 'mws-eu.amazonservices.com',
37
        'A33AVAJ2PDY3EV' => 'mws-eu.amazonservices.com',
38
        'A19VAU5U5O7RUS' => 'mws-fe.amazonservices.com',
39
        'A39IBJ37TRP1C6' => 'mws.amazonservices.com.au',
40
        'A1VC38T7YXB528' => 'mws.amazonservices.jp',
41
    ];
42
43
    protected $countries = [
44
        'BR' => 'A2Q3Y263D00KWC',
45
        'CA' => 'A2EUQ1WTGCTBG2',
46
        'MX' => 'A1AM78C64UM0Y8',
47
        'US' => 'ATVPDKIKX0DER',
48
        'AE' => 'A2VIGQ35RCS4UG',
49
        'DE' => 'A1PA6795UKMFR9',
50
        'EG' => 'ARBP9OOSHTCHU',
51
        'ES' => 'A1RKKUPIHCS9HS',
52
        'FR' => 'A13V1IB3VIYZZH',
53
        'GB' => 'A1F83G8C2ARO7P',
54
        'IN' => 'A21TJRUUN4KGV',
55
        'IT' => 'APJ6JRA9NG5V4',
56
        'SA' => 'A17E79C6D8DWNP',
57
        'TR' => 'A33AVAJ2PDY3EV',
58
        'SG' => 'A19VAU5U5O7RUS',
59
        'AU' => 'A39IBJ37TRP1C6',
60
        'JP' => 'A1VC38T7YXB528',
61
    ];
62
63
    public function __construct(Client $client = null)
64
    {
65
        $this->accessKeyId = config('amazon-mws.access_key_id');
66
        $this->secretKey = config('amazon-mws.secret_key');
67
        $this->sellerId = config('amazon-mws.seller_id');
68
        $this->marketPlaces = ['DE'];
69
        $this->client = $client ?: new Client(['timeout'  => 60]);
70
    }
71
72
    public function setMarketPlaces($countryCodes)
73
    {
74
        $countryCodes = is_array($countryCodes) ? $countryCodes : func_get_args();
75
        $this->marketPlaces = $countryCodes;
76
    }
77
78
    public function getCurrentMarketPlaces()
79
    {
80
        return $this->marketPlaces;
81
    }
82
83
    public function getTimeStamp()
84
    {
85
        return gmdate(self::DATE_FORMAT, time());
86
    }
87
88
    public function getAccessKeyId()
89
    {
90
        return $this->accessKeyId;
91
    }
92
93
    public function setAccessKeyId($key)
94
    {
95
        $this->accessKeyId = $key;
96
97
        return $this;
98
    }
99
100
    public function getSellerId()
101
    {
102
        return $this->sellerId;
103
    }
104
105
    public function setSellerId($id)
106
    {
107
        $this->sellerId = $id;
108
109
        return $this;
110
    }
111
112
    public function getSignatureMethod()
113
    {
114
        return self::SIGNATURE_METHOD;
115
    }
116
117
    public function getSignatureVersion()
118
    {
119
        return self::SIGNATURE_VERSION;
120
    }
121
122
    public function getDomain()
123
    {
124
        $mainMarketPlace = $this->marketPlaces[0];
125
        if ($mainMarketPlace) {
126
            $marketPlaceId = $this->countries[$mainMarketPlace];
127
            return $this->marketplaceIds[$marketPlaceId];
128
        }
129
130
        throw new CountryIsMissingException();
131
    }
132
133
    public function getCountry()
134
    {
135
        return $this->country;
0 ignored issues
show
Bug introduced by
The property country does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
    }
137
138
    public function setCountry($country)
139
    {
140
        if (in_array($country, array_keys($this->countries))) {
141
            $this->country = $country;
142
143
            return $this;
144
        } else {
145
            throw new CountryNotAvailableException();
146
        }
147
    }
148
149
    public function getMarketPlaceIds()
150
    {
151
        return $this->marketplaceIds;
152
    }
153
154
    public function post($action, $path, $version, $params = [], $body = null)
155
    {
156
        $headers = [
157
            'Accept' => 'application/xml',
158
            'x-amazon-user-agent' => self::APPLICATION_NAME.'/'.self::APPLICATION_VERSION,
159
        ];
160
161
        if ($action === 'SubmitFeed') {
162
            $headers['Content-Type'] = 'text/xml; charset=iso-8859-1';
163
        }
164
165
        $requestOptions = [
166
            'headers' => $headers,
167
            'body' => $body,
168
            'query' => $this->getQuery($path, $action, $version, $params),
169
        ];
170
171
        $uri = 'https://'.$this->getDomain().$path;
172
        $response = $this->client->post($uri, $requestOptions);
173
        $xmlResponse = simplexml_load_string($response->getBody()->getContents());
174
        $json = json_encode($xmlResponse);
175
176
        return json_decode($json, true);
177
    }
178
179
    public function getDefaultQueryParams($action, $version, $params = [])
180
    {
181
        $queryParameters = [
182
            'Action' => $action,
183
            'Timestamp' => $this->getTimeStamp(),
184
            'AWSAccessKeyId' => $this->getAccessKeyId(),
185
            'SellerId' => $this->getSellerId(),
186
            'SignatureMethod' => $this->getSignatureMethod(),
187
            'SignatureVersion' => $this->getSignatureVersion(),
188
            'Version' => $version,
189
        ];
190
        $queryParameters = array_merge($queryParameters, $this->getMarketPlaceParams());
191
        $queryParameters = array_merge($queryParameters, $params);
192
        ksort($queryParameters);
193
194
        return $queryParameters;
195
    }
196
197
    public function getMarketPlaceParams()
198
    {
199
        $params = [];
200
        foreach ($this->marketPlaces as $index => $marketPlace) {
201
            $keyName = 'MarketplaceId.Id.'.($index + 1);
202
            $params[$keyName] = $this->countries[$marketPlace];
203
        }
204
205
        return $params;
206
    }
207
208
    public function generateRequestUri($action, $version, $params = [])
209
    {
210
        return http_build_query($this->getDefaultQueryParams($action, $version, $params), null, '&', PHP_QUERY_RFC3986);
211
    }
212
213
    public function getQueryStringForSignature($path, $action, $version, $params = [])
214
    {
215
        return  'POST'
216
                    ."\n"
217
                    .$this->getDomain()
218
                    ."\n"
219
                    .$path
220
                    ."\n"
221
                    .$this->generateRequestUri($action, $version, $params);
222
    }
223
224
    public function generateSignature($path, $action, $version, $params = [])
225
    {
226
        $signature = base64_encode(
227
            hash_hmac(
228
                'sha256',
229
                $this->getQueryStringForSignature($path, $action, $version, $params),
230
                $this->secretKey,
231
                true
232
            )
233
        );
234
235
        return $signature;
236
    }
237
238
    public function getQuery($path, $action, $version, $params = [])
239
    {
240
        $queryParameters = $this->getDefaultQueryParams($action, $version, $params);
241
        $queryParameters['Signature'] = $this->generateSignature($path, $action, $version, $params);
242
243
        return $queryParameters;
244
    }
245
}
246