Skip to content

Commit

Permalink
Merge pull request #3 from bluefyn-international/feature/add-warehous…
Browse files Browse the repository at this point in the history
…e-endpoints

Added warehouse endpoints
  • Loading branch information
Quentin Schmick authored Jan 28, 2022
2 parents 5813d5a + 648442d commit 4d2af4a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/DTO/Warehouse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BluefynInternational\ShipEngine\DTO;

use BluefynInternational\ShipEngine\DTO\Validators\Regex;
use Spatie\DataTransferObject\DataTransferObject;

class Warehouse extends DataTransferObject
{
#[Regex('/^se(-[a-z0-9]+)+$/')]
public string $warehouse_id;
#[Regex('/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[-+]\d{2}:\d{2}))?$/')]
public string $name;
public string $created_at;
public Address $origin_address;
public Address $return_address;
}
2 changes: 2 additions & 0 deletions src/ShipEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use BluefynInternational\ShipEngine\Traits\Shipments;
use BluefynInternational\ShipEngine\Traits\Tags;
use BluefynInternational\ShipEngine\Traits\Tracking;
use BluefynInternational\ShipEngine\Traits\Warehouse;
use Exception;
use GuzzleHttp\Exception\GuzzleException;

Expand All @@ -19,6 +20,7 @@ class ShipEngine
use Shipments;
use Tags;
use Tracking;
use Warehouse;

/**
* ShipEngine SDK Version
Expand Down
135 changes: 135 additions & 0 deletions src/Traits/Warehouse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace BluefynInternational\ShipEngine\Traits;

use BluefynInternational\ShipEngine\DTO\Warehouse as WarehouseDTO;
use BluefynInternational\ShipEngine\ShipEngineClient;
use BluefynInternational\ShipEngine\ShipEngineConfig;
use GuzzleHttp\Exception\GuzzleException;
use Spatie\DataTransferObject\Exceptions\UnknownProperties;

trait Warehouse
{
/**
* @see https://shipengine.github.io/shipengine-openapi/#operation/list_warehouses
*
* @throws GuzzleException|UnknownProperties
*/
public function listWarehouse(
array|ShipEngineConfig $config = null,
) : array {
$config = $this->config->merge($config);
$response = ShipEngineClient::get(
'warehouses',
$config,
);

if ($config->asObject) {
$warehouse_objects = [];
foreach ($response['warehouses'] as $warehouse) {
$warehouse_objects[] = new WarehouseDTO($warehouse);
}
$response['warehouses'] = $warehouse_objects;
}

return $response;
}

/**
* @see https://shipengine.github.io/shipengine-openapi/#operation/create_warehouse
*
* @throws GuzzleException|UnknownProperties
*/
public function createWarehouse(
string $name,
array $origin_address,
array $return_address = [],
array|ShipEngineConfig $config = null,
) : array|WarehouseDTO {
$config = $this->config->merge($config);
$payload = [
'name' => $name,
'origin_address' => $origin_address,
];
if ($return_address) {
$payload['return_address'] = $return_address;
}

$response = ShipEngineClient::post(
'warehouses',
$config,
$payload,
);

if ($config->asObject) {
return new WarehouseDTO($response);
}

return $response;
}

/**
* @see https://shipengine.github.io/shipengine-openapi/#operation/get_warehouse_by_id
*
* @throws GuzzleException|UnknownProperties
*/
public function getWarehouseById(
string $warehouse_id,
array|ShipEngineConfig $config = null,
) : array|WarehouseDTO {
$config = $this->config->merge($config);

$response = ShipEngineClient::get(
"warehouses/{$warehouse_id}",
$config,
);

if ($config->asObject) {
return new WarehouseDTO($response);
}

return $response;
}

/**
* @see https://shipengine.github.io/shipengine-openapi/#operation/update_warehouse
*
* @throws GuzzleException
*/
public function updateWarehouseById(
string $warehouse_id,
string $name,
array $origin_address,
array $return_address = [],
array|ShipEngineConfig $config = null,
) : array {
$payload = [
'name' => $name,
'origin_address' => $origin_address,
];
if ($return_address) {
$payload['return_address'] = $return_address;
}

return ShipEngineClient::put(
"warehouses/{$warehouse_id}",
$this->config->merge($config),
$payload,
);
}

/**
* @see https://shipengine.github.io/shipengine-openapi/#operation/create_warehouse
*
* @throws GuzzleException
*/
public function deleteWarehouseById(
string $warehouse_id,
array|ShipEngineConfig $config = null,
) : array {
return ShipEngineClient::delete(
"warehouses/{$warehouse_id}",
$this->config->merge($config),
);
}
}

0 comments on commit 4d2af4a

Please sign in to comment.