-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.1.7 Check API key before creating a shipment
- Loading branch information
Reindert
committed
Jul 17, 2017
1 parent
ede441e
commit 58a234b
Showing
4 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* A services to check if the API-key is correct | ||
* | ||
* If you want to add improvements, please create a fork in our GitHub: | ||
* https://github.com/myparcelnl | ||
* | ||
* @author Reindert Vetter <[email protected]> | ||
* @copyright 2010-2017 MyParcel | ||
* @license http://creativecommons.org/licenses/by-nc-nd/3.0/nl/deed.en_US CC BY-NC-ND 3.0 NL | ||
* @link https://github.com/myparcelnl/sdk | ||
* @since File available since Release v1.1.7 | ||
*/ | ||
|
||
namespace MyParcelNL\Sdk\src\Services; | ||
|
||
use MyParcelNL\Sdk\src\Model\MyParcelRequest; | ||
|
||
class CheckApiKeyService | ||
{ | ||
private $api_key; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getApiKey() | ||
{ | ||
return $this->api_key; | ||
} | ||
|
||
/** | ||
* @param mixed $api_key | ||
*/ | ||
public function setApiKey($api_key) | ||
{ | ||
$this->api_key = $api_key; | ||
|
||
return $this; | ||
} | ||
|
||
public function apiKeyIsCorrect() | ||
{ | ||
try { | ||
$request = (new MyParcelRequest()); | ||
|
||
$userAgent = $request->getUserAgentFromComposer(); | ||
$request | ||
->setUserAgent($userAgent) | ||
->setRequestParameters( | ||
$this->getApiKey(), | ||
'', | ||
MyParcelRequest::REQUEST_HEADER_RETRIEVE_SHIPMENT | ||
) | ||
->sendRequest('GET'); | ||
|
||
if ($request->getResult() === null) { | ||
throw new \Exception('Unable to connect to MyParcel.'); | ||
} | ||
} catch (\Exception $exception) { | ||
if (strpos($exception, 'Access Denied') > 1) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |