Skip to content

Commit

Permalink
Merge pull request #137 from MyParcelCOM/develop
Browse files Browse the repository at this point in the history
🚀
  • Loading branch information
M4tini authored Dec 31, 2020
2 parents bf6cf2d + 95fcce9 commit 7fd4a5b
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function createShipment(ShipmentInterface $shipment)
throw $exception;
}

return $this->postResource($shipment);
return $this->postResource($shipment, $shipment->getMeta());
}

/**
Expand All @@ -492,7 +492,7 @@ public function updateShipment(ShipmentInterface $shipment)
throw $exception;
}

return $this->patchResource($shipment);
return $this->patchResource($shipment, $shipment->getMeta());
}

/**
Expand Down Expand Up @@ -764,40 +764,46 @@ public function getResourcesFromUri($uri)
* Patch given resource and return the resource that was returned by the request.
*
* @param ResourceInterface $resource
* @param array $meta
* @return ResourceInterface|null
* @throws RequestException
*/
protected function patchResource(ResourceInterface $resource)
protected function patchResource(ResourceInterface $resource, $meta = [])
{
return $this->sendResource($resource, 'patch');
return $this->sendResource($resource, 'patch', $meta);
}

/**
* Post given resource and return the resource that was returned by the request.
*
* @param ResourceInterface $resource
* @param array $meta
* @return ResourceInterface|null
* @throws RequestException
*/
protected function postResource(ResourceInterface $resource)
protected function postResource(ResourceInterface $resource, $meta = [])
{
return $this->sendResource($resource);
return $this->sendResource($resource, 'post', $meta);
}

/**
* Send given resource to the API and return the resource that was returned.
*
* @param ResourceInterface $resource
* @param string $method
* @param array $meta
* @return ResourceInterface|null
* @throws RequestException
*/
protected function sendResource(ResourceInterface $resource, $method = 'post')
protected function sendResource(ResourceInterface $resource, $method = 'post', $meta = [])
{
$response = $this->doRequest(
$this->getResourceUri($resource->getType(), $resource->getId()),
$method,
['data' => $resource],
array_filter([
'data' => $resource,
'meta' => array_filter($meta),
]),
$this->authenticator->getAuthorizationHeader() + [
AuthenticatorInterface::HEADER_ACCEPT => AuthenticatorInterface::MIME_TYPE_JSONAPI,
]
Expand Down
21 changes: 21 additions & 0 deletions src/Resources/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Carrier implements CarrierInterface
const ATTRIBUTE_NAME = 'name';
const ATTRIBUTE_CODE = 'code';
const ATTRIBUTE_CREDENTIALS_FORMAT = 'credentials_format';
const ATTRIBUTE_LABEL_MIME_TYPES = 'label_mime_types';

/** @var string */
private $id;
Expand All @@ -27,6 +28,7 @@ class Carrier implements CarrierInterface
self::ATTRIBUTE_NAME => null,
self::ATTRIBUTE_CODE => null,
self::ATTRIBUTE_CREDENTIALS_FORMAT => [],
self::ATTRIBUTE_LABEL_MIME_TYPES => [],
];

/**
Expand Down Expand Up @@ -113,6 +115,25 @@ public function getCredentialsFormat()
return $this->attributes[self::ATTRIBUTE_CREDENTIALS_FORMAT];
}

/**
* @param array $labelMimeTypes
* @return $this
*/
public function setLabelMimeTypes(array $labelMimeTypes)
{
$this->attributes[self::ATTRIBUTE_LABEL_MIME_TYPES] = $labelMimeTypes;

return $this;
}

/**
* @return string
*/
public function getLabelMimeTypes()
{
return $this->attributes[self::ATTRIBUTE_LABEL_MIME_TYPES];
}

/**
* This function puts all object properties in an array and returns it.
*
Expand Down
1 change: 1 addition & 0 deletions src/Resources/Interfaces/FileInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface FileInterface extends ResourceInterface
const MIME_TYPE_PNG = 'image/png';
const MIME_TYPE_JPG = 'image/jpeg';
const MIME_TYPE_PDF = 'application/pdf';
const MIME_TYPE_ZPL = 'application/zpl';

const FORMAT_MIME_TYPE = 'mime_type';
const FORMAT_EXTENSION = 'extension';
Expand Down
19 changes: 19 additions & 0 deletions src/Resources/Proxy/CarrierProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ public function getCredentialsFormat()
return $this->getResource()->getCredentialsFormat();
}

/**
* @param array $labelMimeTypes
* @return $this
*/
public function setLabelMimeTypes(array $labelMimeTypes)
{
$this->getResource()->setLabelMimeTypes($labelMimeTypes);

return $this;
}

/**
* @return array
*/
public function getLabelMimeTypes()
{
return $this->getResource()->getLabelMimeTypes();
}

/**
* This function puts all object properties in an array and returns it.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/Proxy/ShipmentProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function getType()
return $this->type;
}

/**
* @return array
*/
public function getMeta()
{
return $this->getResource()->getMeta();
}

/**
* @param AddressInterface $recipientAddress
* @return $this
Expand Down Expand Up @@ -711,6 +719,14 @@ public function clearTags()
return $this->getResource()->clearTags();
}

/**
* {@inheritDoc}
*/
public function setLabelMimeType($labelMimeType)
{
return $this->getResource()->setLabelMimeType($labelMimeType);
}

/**
* This function puts all object properties in an array and returns it.
*
Expand Down
22 changes: 21 additions & 1 deletion src/Resources/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Shipment implements ShipmentInterface

/** @var array */
private $meta = [
self::META_LABEL_MIME_TYPE => 'application/pdf',
self::META_LABEL_MIME_TYPE => FileInterface::MIME_TYPE_PDF,
self::META_SERVICE_CODE => null,
];

Expand Down Expand Up @@ -148,6 +148,14 @@ public function getType()
return $this->type;
}

/**
* @return array
*/
public function getMeta()
{
return $this->meta;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -834,4 +842,16 @@ public function clearTags()

return $this;
}

/**
* Supported values are FileInterface::MIME_TYPE_PDF or FileInterface::MIME_TYPE_ZPL
* @param $labelMimeType
* @return $this
*/
public function setLabelMimeType($labelMimeType)
{
$this->meta[self::META_LABEL_MIME_TYPE] = $labelMimeType;

return $this;
}
}
1 change: 1 addition & 0 deletions tests/Feature/MyParcelComApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function testGetCarriers()
$this->assertNotEmpty($carrier->getName());
$this->assertNotEmpty($carrier->getCode());
$this->assertNotEmpty($carrier->getCredentialsFormat());
$this->assertNotEmpty($carrier->getLabelMimeTypes());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"type": "string"
}
}
}
},
"label_mime_types": [
"application/pdf"
]
},
"links": {
"self": "https://api/carriers/eef00b32-177e-43d3-9b26-715365e4ce46"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"type": "string"
}
}
}
},
"label_mime_types": [
"application/pdf"
]
},
"links": {
"self": "https://api/carriers/eef00b32-177e-43d3-9b26-715365e4ce46"
Expand All @@ -46,7 +49,10 @@
"type": "string"
}
}
}
},
"label_mime_types": [
"application/pdf"
]
},
"links": {
"self": "https://api/carriers/4a78637a-5d81-4e71-9b18-c338968f72fa"
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/ShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ public function testTags()
$this->assertNull($shipment->getTags());
}

/** @test */
public function testLabelMimeType()
{
$shipment = new Shipment();

$this->assertEquals(FileInterface::MIME_TYPE_PDF, $shipment->jsonSerialize()['meta']['label_mime_type']);

$shipment->setLabelMimeType(FileInterface::MIME_TYPE_ZPL);

$this->assertEquals(FileInterface::MIME_TYPE_ZPL, $shipment->jsonSerialize()['meta']['label_mime_type']);
}

/** @test */
public function testJsonSerialize()
{
Expand Down

0 comments on commit 7fd4a5b

Please sign in to comment.