Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpdplugin committed Jul 11, 2019
1 parent f236b25 commit 8959897
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tests/etc/parameters.yml
phpunit.xml
.php_cs.cache
.idea
.history
25 changes: 25 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use DpdConnect\Sdk\Resources\Parcelshop;
use DpdConnect\Sdk\Resources\Shipment;
use DpdConnect\Sdk\Resources\Country;
use DpdConnect\Sdk\Resources\Parcel;
use DpdConnect\Sdk\Resources\Job;

class Client
{
Expand Down Expand Up @@ -35,6 +37,11 @@ class Client
*/
public $country;

/**
* @var Parcel
*/
public $parcel;

/**
* @var HttpClient
*/
Expand Down Expand Up @@ -68,6 +75,8 @@ public function __construct(
$this->shipment = new Shipment($this->resourceClient);
$this->parcelshop = new Parcelshop($this->resourceClient);
$this->country = new Country($this->resourceClient);
$this->parcel = new Parcel($this->resourceClient);
$this->job = new Job($this->resourceClient);
}

/**
Expand Down Expand Up @@ -101,4 +110,20 @@ public function getCountries()
{
return $this->country;
}

/**
* @return Parcel
*/
public function getParcel()
{
return $this->parcel;
}

/**
* @return Job
*/
public function getJob()
{
return $this->job;
}
}
4 changes: 3 additions & 1 deletion src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ClientBuilder implements ClientBuilderInterface
/**
* @param string $endpoint
*/
public function __construct($endpoint = null, $meta = [])
public function __construct($endpoint = null, $meta = null)
{
if (!is_null($endpoint) && $endpoint !== '') {
$this->endpoint = $endpoint;
Expand All @@ -54,6 +54,7 @@ private function getHttpClient()
{
if (null === $this->httpClient) {
$this->httpClient = new HttpClient($this->endpoint);
$this->httpClient->setMeta($this->meta);
}

return $this->httpClient;
Expand All @@ -66,6 +67,7 @@ private function getHttpClient()
public function setHttpClient(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
$this->httpClient->setMeta($this->meta);
return $this;
}

Expand Down
47 changes: 20 additions & 27 deletions src/Common/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DpdConnect\Sdk\Common;
use DpdConnect\Sdk\Exceptions;
use DpdConnect\Sdk\Exceptions\HttpException;
use DpdConnect\Sdk\Objects\MetaData;
use DpdConnect\Sdk\Objects\ObjectFactory;
use InvalidArgumentException;

class HttpClient implements HttpClientInterface
Expand Down Expand Up @@ -54,7 +56,7 @@ class HttpClient implements HttpClientInterface
private $httpOptions = [];

/**
* @var array
* @var MetaData
*/
private $meta;

Expand Down Expand Up @@ -91,9 +93,21 @@ public function __construct($endpoint, $timeout = 10, $connectionTimeout = 10, $
}

/**
* @param array $meta
* @return Metadata
*/
private function getMeta()
{
if (!$this->meta) {
$this->meta = ObjectFactory::create(MetaData::class, []);
}

return $this->meta;
}

/**
* @param MetaData $meta
*/
public function setMeta(array $meta = [])
public function setMeta($meta = null)
{
$this->meta = $meta;
}
Expand Down Expand Up @@ -174,21 +188,15 @@ public function sendRequest($method, $resourceName, $query = null, $headers = []
{
$curl = curl_init();

// if ($this->authentication === null) {
// throw new Exceptions\AuthenticateException('Can not perform API Request without Authentication');
// }

list($webshopType, $webshopVersion, $pluginVersion) = $this->parseMeta();

$baseHeaders = [
'User-agent: ' . implode(' ', $this->userAgent),
'Accept: application/json',
'Content-Type: application/json',
'Accept-Charset: utf-8',
'x-php-version: ' . $this->getPhpVersion(),
'x-webshop-type: ' . $webshopType,
'x-webshop-version: ' . $webshopVersion,
'x-plugin-version: ' . $pluginVersion,
'x-webshop-type: ' . $this->getMeta()->getWebshopType(),
'x-webshop-version: ' . $this->getMeta()->getWebshopVersion(),
'x-plugin-version: ' . $this->getMeta()->getPluginVersion(),
'x-sdk-version: ' . Client::CLIENT_VERSION,
'x-os: ' . php_uname(),
];
Expand Down Expand Up @@ -254,19 +262,4 @@ private function getPhpVersion()

return 'PHP/' . PHP_VERSION_ID;
}

private function parseMeta()
{
if (!$this->meta) {
$this->meta = [];
}

$meta = $this->meta;

$webshopType = isset($meta['webshopType']) ? $meta['webshopType'] : 'unknown';
$webshopVersion = isset($meta['webshopVersion']) ? $meta['webshopVersion'] : 'unknown';
$pluginVersion = isset($meta['pluginVersion']) ? $meta['pluginVersion'] : 'unknown';

return [$webshopType, $webshopVersion, $pluginVersion];
}
}
5 changes: 3 additions & 2 deletions src/Common/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use DpdConnect\Sdk\Common;
use DpdConnect\Sdk\Exceptions;
use DpdConnect\Sdk\Objects\MetaData;

interface HttpClientInterface
{
/**
* @param array $meta
* @param MetaData $meta
*/
public function setMeta(array $meta = []);
public function setMeta($meta = null);

/**
* @param string $userAgent
Expand Down
8 changes: 7 additions & 1 deletion src/Common/ResourceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public function getResource($query = [])
return $response;
}

return json_decode($response, true);
$decoded = json_decode($response, true);

if ($decoded === null) {
return $response;
}

return $decoded;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Model/Request/RequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static function getParcels($request)
foreach ($request['packages'] AS $i => $package) {
$parcels[] = ObjectFactory::create(Parcel::class, [
'customerReferences' => [$request['order_shipment']->getOrder()->getIncrementId() . ' #' . $i],
'weight' => (int) round($package['params']['weight'] / count($request['packages'])),
'weight' => (int) round($request['order_shipment']->getOrder()->getWeight()),
]);
}

Expand Down Expand Up @@ -189,10 +189,11 @@ private static function getCustoms($request)
private static function getShipments($request)
{
return ObjectFactory::create(Shipment::class, [
'orderId' => $request['orderId'],
'orderId' => $request['order_shipment']->getOrderId(),
'sendingDepot' => '0522',
'weight' => (int) $request['package_weight'],
'sender' => self::getSender($request),
'notifications' => [],
'receiver' => self::getReceiver($request),
'product' => self::getProduct($request),
'parcels' => self::getParcels($request),
Expand Down Expand Up @@ -251,20 +252,21 @@ public static function mapShipmentRequest($request, $sequenceNumber, $async = fa
$printOptions = self::getPrintOptions($request);

$shipmentOrder = ObjectFactory::create(Shipment::class, [
'orderId' => $request['order_shipment']->getOrder()->getIncrementId(),
'orderId' => $request['order_shipment']->getOrderId(),
'sendingDepot' => '0522',
'customerReferences' => [$request['order_shipment']->getOrder()->getIncrementId() . ' #1'],
'weight' => (int) ($request['package_weight'] * 100),
'sender' => self::getSender($request),
'notifications' => [],
'receiver' => self::getReceiver($request),
'product' => self::getProduct($request),
'parcels' => self::getParcels($request),
'customs' => self::getCustoms($request)
]);

//
// var_dump($request);
// die;
// var_dump($request);
// die;


return $shipmentOrder;
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Response/ShipmentResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function parseShipmentResponse($response)

return $labels;
} else {
print_r($content);
return $content;
}
}
}
Expand Down
77 changes: 77 additions & 0 deletions src/Objects/MetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace DpdConnect\Sdk\Objects;

class MetaData extends BaseObject
{
const DEFAULTVERSIONSTRING = 'UNKNOWN';

/**
* @var string
*/
protected $webshopType;

/**
* @var string
*/
protected $webshopVersion;

/**
* @var string
*/
protected $pluginVersion;

/**
* @return string
*/
public function getWebshopType()
{
return $this->webshopType ? $this->webshopType : self::DEFAULTVERSIONSTRING;
}

/**
* @param string $webshopType
* @return Metadata
*/
public function setWebshopType($webshopType)
{
$this->webshopType = $webshopType;
return $this;
}

/**
* @return string
*/
public function getWebshopVersion()
{
return $this->webshopVersion ? $this->webshopVersion : self::DEFAULTVERSIONSTRING;
}

/**
* @param string $webshopVersion
* @return Metadata
*/
public function setWebshopVersion($webshopVersion)
{
$this->webshopVersion = $webshopVersion;
return $this;
}

/**
* @return string
*/
public function getPluginVersion()
{
return $this->pluginVersion ? $this->pluginVersion : self::DEFAULTVERSIONSTRING;
}

/**
* @param string $pluginVersion
* @return Metadata
*/
public function setPluginVersion($pluginVersion)
{
$this->pluginVersion = $pluginVersion;
return $this;
}
}
Loading

0 comments on commit 8959897

Please sign in to comment.