Skip to content

Commit

Permalink
✨ Added getOne collection feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
NickVries committed Feb 15, 2024
1 parent b3f7c83 commit 9a53b38
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/MyParcelComApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ public function getCollections(array $filters = [], int $ttl = self::TTL_10MIN):
return $this->getRequestCollection($url->getUrl(), $ttl);
}

public function getCollection(string $collectionId, int $ttl = self::TTL_NO_CACHE): Resources\Interfaces\CollectionInterface
{
return $this->getResourceById(ResourceInterface::TYPE_COLLECTION, $collectionId, $ttl);
}

// TODO: Add other collection methods.

/**
Expand Down
35 changes: 23 additions & 12 deletions src/MyParcelComApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace MyParcelCom\ApiSdk;

use MyParcelCom\ApiSdk\Authentication\AuthenticatorInterface;
use MyParcelCom\ApiSdk\Collection\CollectionInterface;
use MyParcelCom\ApiSdk\Collection\CollectionInterface as ResourceCollectionInterface;
use MyParcelCom\ApiSdk\Exceptions\MyParcelComException;
use MyParcelCom\ApiSdk\Http\Exceptions\RequestException;
use MyParcelCom\ApiSdk\Resources\Interfaces\CarrierInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\CollectionInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\FileInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\ManifestInterface;
use MyParcelCom\ApiSdk\Resources\Interfaces\ResourceFactoryInterface;
Expand Down Expand Up @@ -52,14 +53,14 @@ public function authenticate(AuthenticatorInterface $authenticator): self;
/**
* @deprecated
*/
public function getRegions(array $filters = [], int $ttl = self::TTL_10MIN): CollectionInterface;
public function getRegions(array $filters = [], int $ttl = self::TTL_10MIN): ResourceCollectionInterface;

/**
* Get all the carriers from the API.
*
* @throws MyParcelComException
*/
public function getCarriers(int $ttl = self::TTL_10MIN): CollectionInterface;
public function getCarriers(int $ttl = self::TTL_10MIN): ResourceCollectionInterface;

/**
* Get the pick-up/drop-off locations around a given location.
Expand All @@ -73,14 +74,14 @@ public function getPickUpDropOffLocations(
CarrierInterface $specificCarrier = null,
bool $onlyActiveContracts = true,
int $ttl = self::TTL_10MIN,
): CollectionInterface|array;
): ResourceCollectionInterface|array;

/**
* Get the shops from the API.
*
* @throws MyParcelComException
*/
public function getShops(int $ttl = self::TTL_10MIN): CollectionInterface;
public function getShops(int $ttl = self::TTL_10MIN): ResourceCollectionInterface;

/**
* Get the default shop that will be used when interacting with the API and no specific shop has been set.
Expand All @@ -99,14 +100,14 @@ public function getServices(
ShipmentInterface $shipment = null,
array $filters = ['has_active_contract' => 'true'],
int $ttl = self::TTL_10MIN,
): CollectionInterface;
): ResourceCollectionInterface;

/**
* Get all the services that are available for the given carrier.
*
* @throws MyParcelComException
*/
public function getServicesForCarrier(CarrierInterface $carrier, int $ttl = self::TTL_10MIN): CollectionInterface;
public function getServicesForCarrier(CarrierInterface $carrier, int $ttl = self::TTL_10MIN): ResourceCollectionInterface;

/**
* Retrieves service rates based on the set filters. Available filters are: service, contract and weight. Note that
Expand All @@ -116,7 +117,7 @@ public function getServicesForCarrier(CarrierInterface $carrier, int $ttl = self
public function getServiceRates(
array $filters = ['has_active_contract' => 'true'],
int $ttl = self::TTL_10MIN,
): CollectionInterface;
): ResourceCollectionInterface;

/**
* Retrieves service rates based on the shipment.
Expand All @@ -125,7 +126,7 @@ public function getServiceRates(
public function getServiceRatesForShipment(
ShipmentInterface $shipment,
int $ttl = self::TTL_10MIN,
): CollectionInterface;
): ResourceCollectionInterface;

/**
* Retrieve dynamic rates (price / options / availability) from the carrier, based on the provided shipment data.
Expand All @@ -144,7 +145,7 @@ public function resolveDynamicServiceRates(
*
* @throws MyParcelComException
*/
public function getShipments(ShopInterface $shop = null, int $ttl = self::TTL_NO_CACHE): CollectionInterface;
public function getShipments(ShopInterface $shop = null, int $ttl = self::TTL_NO_CACHE): ResourceCollectionInterface;

/**
* Get a specific shipment from the API.
Expand Down Expand Up @@ -200,7 +201,7 @@ public function createAndRegisterShipment(
*
* @throws MyParcelComException
*/
public function getManifests(int $ttl = self::TTL_NO_CACHE): CollectionInterface;
public function getManifests(int $ttl = self::TTL_NO_CACHE): ResourceCollectionInterface;

/**
* Get a specific manifest from the API.
Expand All @@ -216,7 +217,17 @@ public function createManifest(ManifestInterface $manifest): ManifestInterface;

public function getManifestFile(string $manifestId, string $fileId): FileInterface;

public function getCollections(): CollectionInterface;
/**
* @param array{
* collection_date?: string,
* shop?: string,
* status?: string,
* carrier?: string
* } $filters
*/
public function getCollections(array $filters, int $ttl = self::TTL_10MIN): ResourceCollectionInterface;

public function getCollection(string $collectionId, int $ttl = self::TTL_NO_CACHE): CollectionInterface;

// TODO: Add other collection methods.

Expand Down
15 changes: 11 additions & 4 deletions tests/Feature/MyParcelComApi/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ public function testItRetrievesCollections(): void

public function testItFiltersCollections(): void
{
$filters = [
$collections = $this->api->getCollections([
'shop' => '1ebabb0e-9036-4259-b58e-2b42742bb86a',
'collection_date' => '2024-02-17',
];

$collections = $this->api->getCollections($filters);
]);

$this->assertInstanceOf(ResourceCollectionInterface::class, $collections);
$this->assertCount(2, $collections);
Expand All @@ -51,4 +49,13 @@ public function testItFiltersCollections(): void
'Test collection 2',
], $collectionDescriptions);
}

public function testItRetrievesASingleCollection(): void
{
$collection = $this->api->getCollection('8d8d63aa-032b-4674-990b-706551a2bf23');

$this->assertInstanceOf(CollectionInterface::class, $collection);
$this->assertEquals('8d8d63aa-032b-4674-990b-706551a2bf23', $collection->getId());
$this->assertEquals('Test', $collection->getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"id": "ea3058b5-0aee-4d32-b3b2-6b4022a4de00",
"id": "8d8d63aa-032b-4674-990b-706551a2bf23",
"type": "collections",
"attributes": {
"description": "Test",
Expand All @@ -24,7 +24,7 @@
"created_at": 1707991769
},
"links": {
"self": "https:\/\/api.localhost.private\/collections\/ea3058b5-0aee-4d32-b3b2-6b4022a4de00"
"self": "https:\/\/api.localhost.private\/collections\/8d8d63aa-032b-4674-990b-706551a2bf23"
},
"relationships": {
"shop": {
Expand Down

0 comments on commit 9a53b38

Please sign in to comment.