diff --git a/.gitignore b/.gitignore index c027652..ef05027 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tests/etc/parameters.yml phpunit.xml .php_cs.cache .idea +.history diff --git a/src/Client.php b/src/Client.php index 8e38814..edccb76 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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 { @@ -35,6 +37,11 @@ class Client */ public $country; + /** + * @var Parcel + */ + public $parcel; + /** * @var HttpClient */ @@ -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); } /** @@ -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; + } } diff --git a/src/ClientBuilder.php b/src/ClientBuilder.php index ba2dfd5..1f91bea 100644 --- a/src/ClientBuilder.php +++ b/src/ClientBuilder.php @@ -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; @@ -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; @@ -66,6 +67,7 @@ private function getHttpClient() public function setHttpClient(HttpClient $httpClient) { $this->httpClient = $httpClient; + $this->httpClient->setMeta($this->meta); return $this; } diff --git a/src/Common/HttpClient.php b/src/Common/HttpClient.php index 5386dec..0b4fbbc 100644 --- a/src/Common/HttpClient.php +++ b/src/Common/HttpClient.php @@ -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 @@ -54,7 +56,7 @@ class HttpClient implements HttpClientInterface private $httpOptions = []; /** - * @var array + * @var MetaData */ private $meta; @@ -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; } @@ -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(), ]; @@ -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]; - } } diff --git a/src/Common/HttpClientInterface.php b/src/Common/HttpClientInterface.php index 78b200d..75eb93a 100644 --- a/src/Common/HttpClientInterface.php +++ b/src/Common/HttpClientInterface.php @@ -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 diff --git a/src/Common/ResourceClient.php b/src/Common/ResourceClient.php index 58b4c7c..d5501b3 100644 --- a/src/Common/ResourceClient.php +++ b/src/Common/ResourceClient.php @@ -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; } /** diff --git a/src/Model/Request/RequestMapper.php b/src/Model/Request/RequestMapper.php index 38fb52a..6b7d5cd 100644 --- a/src/Model/Request/RequestMapper.php +++ b/src/Model/Request/RequestMapper.php @@ -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()), ]); } @@ -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), @@ -251,11 +252,12 @@ 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), @@ -263,8 +265,8 @@ public static function mapShipmentRequest($request, $sequenceNumber, $async = fa ]); // -// var_dump($request); -// die; + // var_dump($request); + // die; return $shipmentOrder; diff --git a/src/Model/Response/ShipmentResponseParser.php b/src/Model/Response/ShipmentResponseParser.php index aa80a62..a9277ca 100644 --- a/src/Model/Response/ShipmentResponseParser.php +++ b/src/Model/Response/ShipmentResponseParser.php @@ -62,7 +62,7 @@ public static function parseShipmentResponse($response) return $labels; } else { - print_r($content); + return $content; } } } diff --git a/src/Objects/MetaData.php b/src/Objects/MetaData.php new file mode 100644 index 0000000..e5ae34c --- /dev/null +++ b/src/Objects/MetaData.php @@ -0,0 +1,77 @@ +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; + } +} diff --git a/src/Objects/ShipmentOrder/Shipment/Parcel.php b/src/Objects/ShipmentOrder/Shipment/Parcel.php index 262ab54..d752fb3 100644 --- a/src/Objects/ShipmentOrder/Shipment/Parcel.php +++ b/src/Objects/ShipmentOrder/Shipment/Parcel.php @@ -31,5 +31,95 @@ class Parcel extends BaseObject implements JsonSerializable, ParcelInterface /** * @var CashOnDelivery */ - protected $cashOnDelivery; + protected $cod = false; + + /** + * @return string + */ + public function getParcelLabelNumber() + { + return $this->parcelLabelNumber; + } + + /** + * @param string $parcelLabelNumber + * @return Parcel + */ + public function setParcelLabelNumber($parcelLabelNumber) + { + $this->parcelLabelNumber = $parcelLabelNumber; + return $this; + } + + /** + * @return string[] + */ + public function getCustomerReferences() + { + return $this->customerReferences; + } + + /** + * @param string[] $customerReferences + * @return Parcel + */ + public function setCustomerReferences($customerReferences) + { + $this->customerReferences = $customerReferences; + return $this; + } + + /** + * @return string + */ + public function getVolume() + { + return $this->volume; + } + + /** + * @param string $volume + * @return Parcel + */ + public function setVolume($volume) + { + $this->volume = $volume; + return $this; + } + + /** + * @return int|null + */ + public function getWeight() + { + return $this->weight; + } + + /** + * @param int|null $weight + * @return Parcel + */ + public function setWeight($weight) + { + $this->weight = $weight; + return $this; + } + + /** + * @return CashOnDelivery + */ + public function getCod() + { + return $this->cod; + } + + /** + * @param CashOnDelivery $cod + * @return Parcel + */ + public function setCod($cod) + { + $this->cod = $cod; + return $this; + } } diff --git a/src/Resources/Job.php b/src/Resources/Job.php new file mode 100644 index 0000000..fbdc10d --- /dev/null +++ b/src/Resources/Job.php @@ -0,0 +1,40 @@ +resourceClient->setResourceName('api/connect/v1/job/' . $id); + + return $this->resourceClient->getResource(); + } +} diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php new file mode 100644 index 0000000..bc439f4 --- /dev/null +++ b/src/Resources/Parcel.php @@ -0,0 +1,51 @@ +resourceClient->setResourceName('api/connect/v1/parcel/status/' . $id); + + return $this->resourceClient->getResource(); + } + + public function getLabel($id) + { + if (is_null($id)) { + throw new \InvalidArgumentException('No parcel id provided.'); + } + + $this->resourceClient->setResourceName('api/connect/v1/parcel/label/' . $id); + + return $this->resourceClient->getResource(); + } +}