PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / vendor / myparcelnl / sdk / src / Services / CheckApiKeyService.php

CheckApiKeyService.php in PostNL for WooCommerce 4.0.0, at includes/vendor/myparcelnl/sdk/src/Services/CheckApiKeyService.php

70 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2 /**
3 * A services to check if the API-key is correct
4 *
5 * If you want to add improvements, please create a fork in our GitHub:
6 * https://github.com/myparcelnl
7 *
8 * @author Reindert Vetter <[email protected]>
9 * @copyright 2010-2020 MyParcel
10 * @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL
11 * @link https://github.com/myparcelnl/sdk
12 * @since File available since Release v1.1.7
13 */
14
15 namespace MyParcelNL\Sdk\src\Services;
16
17 use MyParcelNL\Sdk\src\Exception\ApiException;
18 use MyParcelNL\Sdk\src\Model\MyParcelRequest;
19
20 class CheckApiKeyService
21 {
22 private $api_key;
23
24 /**
25 * @return mixed
26 */
27 public function getApiKey()
28 {
29 return $this->api_key;
30 }
31
32 /**
33 * @param mixed $api_key
34 * @return CheckApiKeyService
35 */
36 public function setApiKey($api_key)
37 {
38 $this->api_key = $api_key;
39
40 return $this;
41 }
42
43 public function apiKeyIsCorrect()
44 {
45 try {
46 $request = (new MyParcelRequest());
47
48 $userAgent = $request->getUserAgentFromComposer();
49 $request
50 ->setUserAgent($userAgent)
51 ->setRequestParameters(
52 $this->getApiKey(),
53 null,
54 MyParcelRequest::REQUEST_HEADER_RETRIEVE_SHIPMENT
55 )
56 ->sendRequest('GET');
57
58 if ($request->getResult() === null) {
59 throw new ApiException('Unable to connect to MyParcel.');
60 }
61 } catch (\Exception $exception) {
62 if (strpos($exception->getMessage(), 'Access Denied') > 1) {
63 return false;
64 }
65 }
66
67 return true;
68 }
69 }
70