Skip to content

Commit

Permalink
Remove DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
anvari182 committed Jan 6, 2024
1 parent 3648cde commit 6184224
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 145 deletions.
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ A Laravel package for managing [TrackingMore API](https://www.trackingmore.com/d

TrackingMore helps eCommerce businesses to update and manage their shipment with efficiency.

This package is a wrapper of [TrackingMore API PHP SDK](https://github.com/TrackingMore-API/trackingmore-sdk-php) for Laravel.

## Version support

- **PHP**: 8.0 | 8.1 | 8.2
Expand Down Expand Up @@ -45,10 +47,10 @@ public function __construct(private Tracking $tracking, private Courier $courier
public function index()
{
// Create a tracking
$this->tracking->create(new TrackingData(trackingNumber: 'xyz123', courierCode: 'ups'));
$this->tracking->createTracking(['tracking_number' => 'xyz123', 'courier_code' => 'ups']);

// Get all couriers
$couriers = $this->courier->getAllCourier();
$couriers = $this->courier->getAllCouriers();
}
```

Expand All @@ -58,27 +60,27 @@ Or use it with Facade:
use Anvari182\TrackingMore\Facades\TrackingMore;

// Create a tracking
TrackingMore::tracking()->create(new TrackingData(trackingNumber: 'xyz123', courierCode: 'ups'))
TrackingMore::tracking()->createTracking(['tracking_number' => 'xyz123', 'courier_code' => 'ups'])

// Get all couriers
$couriers = TrackingMore::courier()->getAllCourier();
$couriers = TrackingMore::courier()->getAllCouriers();
```

## Tracking
##### Create a tracking
```php
TrackingMore::tracking()->create(new TrackingData(trackingNumber: 'xyz123', courierCode: 'ups'))
TrackingMore::tracking()->createTracking(['tracking_number' => 'xyz123', 'courier_code' => 'ups'])
```

##### Get results
```php
TrackingMore::tracking()->getResults()
TrackingMore::tracking()->getTrackingResults()
```

##### Create trackings
Create multiple trackings (Max. 40 tracking numbers create in one call).
```php
TrackingMore::tracking()->createMultiple([
TrackingMore::tracking()->batchCreateTrackings([
['tracking_number' => 'xyz1234', 'courier_code' => 'ups'],
['tracking_number' => 'xyz1235', 'courier_code' => 'ups'],
['tracking_number' => 'xyz1236', 'courier_code' => 'ups'],
Expand All @@ -88,37 +90,37 @@ Create multiple trackings (Max. 40 tracking numbers create in one call).
##### Update a tracking by ID
Tracking ID
```php
TrackingMore::tracking()->updateById('13123213213213', ['note' => 'New test order note', 'customer_name'=>'New name'])
TrackingMore::tracking()->updateTrackingByID('13123213213213', ['note' => 'New test order note', 'customer_name'=>'New name'])
```

##### Delete a tracking by ID
Tracking ID
```php
TrackingMore::tracking()->deleteById('13123213213213')
TrackingMore::tracking()->deleteTrackingByID('13123213213213')
```

##### Retrack an expired tracking by ID
Tracking ID
```php
TrackingMore::tracking()->retrackByID('13123213213213')
TrackingMore::tracking()->retrackTrackingByID('13123213213213')
```

## Courier

##### Detect courier
Return a list of matched couriers based on submitted tracking number.
```php
TrackingMore::courier()->detectCourier(['tracking_number' => '9261290312833844954982'])
TrackingMore::courier()->detect(['tracking_number' => '9261290312833844954982'])
```

##### Get all couriers
Return a list of all supported couriers.
```php
TrackingMore::courier()->getAll()
TrackingMore::courier()->getAllCouriers()
```

## Dependencies
[TrackingMore SDK](https://github.com/TrackingMore-API/trackingmore-sdk-php) v.1.0.0
[TrackingMore API PHP SDK](https://github.com/TrackingMore-API/trackingmore-sdk-php) v.1.0.0

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
"require": {
"php": "^8.0|^8.1",
"illuminate/support": "^10.0",
"spatie/laravel-data": "^3.11",
"trackingmore/trackingmore-sdk-php": "^0.1.1"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.5",
"laravel/pint": "^1.3",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^6.3",
"orchestra/testbench": "^7.17",
"orchestra/testbench": "^8.17",
"pestphp/pest": "^1.22",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5"
Expand Down
35 changes: 0 additions & 35 deletions src/Data/TrackingData.php

This file was deleted.

24 changes: 0 additions & 24 deletions src/Helpers/ArrayHelper.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/TrackingMore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Anvari182\TrackingMore;

use TrackingMore\Couriers as Courier;
use TrackingMore\Trackings as Tracking;
use Anvari182\TrackingMore\TrackingMoreRequests\Courier;
use Anvari182\TrackingMore\TrackingMoreRequests\Tracking;

class TrackingMore
{
Expand Down
22 changes: 12 additions & 10 deletions src/TrackingMoreRequests/Courier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace Anvari182\TrackingMore\TrackingMoreRequests;

use Exception;
use TrackingMore\Couriers;
use TrackingMore\Interfaces\CouriersInterface;
use Trackingmore\TrackingMoreException;

class Courier extends Couriers
class Courier implements CouriersInterface
{
/**
* @throws Exception
*/
public function detectCourier(string $trackingNumber): array
public function __construct(private Couriers $couriers)
{
}

public function getAllCouriers(): array
{
return $this->detect(['tracking_number' => $trackingNumber]);
return $this->couriers->getAllCouriers();
}

/**
* @return array
* @throws TrackingMoreException
*/
public function getAll(): array
public function detect($params = []): array
{
return $this->getAllCouriers();
return $this->couriers->detect($params);
}
}
61 changes: 19 additions & 42 deletions src/TrackingMoreRequests/Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,58 @@

namespace Anvari182\TrackingMore\TrackingMoreRequests;

use Anvari182\TrackingMore\Data\TrackingData;
use Anvari182\TrackingMore\Helpers\ArrayHelper;
use Exception;
use TrackingMore\TrackingMoreException;
use TrackingMore\Interfaces\TrackingsInterface;
use Trackingmore\TrackingMoreException;
use TrackingMore\Trackings;

class Tracking extends Trackings
class Tracking implements TrackingsInterface
{
/**
* @param TrackingData $data
* @return array
* @throws TrackingMoreException
*/
public function create(TrackingData $data): array
public function __construct(private Trackings $trackings)
{
return $this->createTracking(ArrayHelper::camelToSnakeKeys($data->toArray()));
}

/**
* @param array $trackingData
* @return array
* @throws TrackingMoreException
* @throws Exception
*/
public function createMultiple(array $trackingData): array
public function createTracking($params = []): array
{
if (count($trackingData) > 40) {
throw new Exception('Max 40 tracking numbers are allowed!');
}

$data = [];

foreach ($trackingData as $tracking) {
$data[] = $tracking->toArray();
}
return $this->trackings->createTracking($params);
}

return $this->batchCreateTrackings($data);
public function getTrackingResults($params = []): array
{
return $this->trackings->getTrackingResults();
}

/**
* @return array
* @throws TrackingMoreException
*/
public function getResults(): array
public function batchCreateTrackings($params = []): array
{
return $this->getTrackingResults();
return $this->trackings->batchCreateTrackings($params);
}

/**
* @param string $id
* @param array $params
* @return array
* @throws TrackingMoreException
*/
public function updateById(string $id, array $params): array
public function updateTrackingByID($idString = '', $params = []): array
{
return $this->updateTrackingByID($id, $params);
return $this->trackings->updateTrackingByID($idString, $params);
}

/**
* @param string $id
* @return array
* @throws TrackingMoreException
*/
public function deleteById(string $id): array
public function deleteTrackingByID($idString = ''): array
{
return $this->deleteTrackingByID($id);
return $this->trackings->deleteTrackingByID($idString);
}

/**
* @param string $id
* @return array
* @throws TrackingMoreException
*/
public function retrackByID(string $id): array
public function retrackTrackingByID($idString = ''): array
{
return $this->retrackTrackingByID($id);
return $this->trackings->retrackTrackingByID($idString);
}
}
6 changes: 4 additions & 2 deletions src/TrackingMoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Anvari182\TrackingMore\TrackingMoreRequests\Tracking;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use TrackingMore\Couriers;
use TrackingMore\Trackings;

class TrackingMoreServiceProvider extends ServiceProvider
{
Expand All @@ -16,11 +18,11 @@ public function register(): void
$apiKey = config('trackingmore.api_key');

$this->app->bind(Tracking::class, function () use ($apiKey) {
return new Tracking($apiKey);
return new Tracking(new Trackings($apiKey));
});

$this->app->bind(Courier::class, function () use ($apiKey) {
return new Courier($apiKey);
return new Courier(new Couriers($apiKey));
});

$this->app->bind(TrackingMore::class, function (Application $app) {
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/CourierWithFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

it('detects a courier', function () {
$courier = mock(Courier::class);
$courier->shouldReceive('detectCourier')->andReturn(json_decode(File::get(__DIR__ . '/../Fixtures/courierDetectResponse.json'), true));
$courier->shouldReceive('detect')->andReturn(json_decode(File::get(__DIR__ . '/../Fixtures/courierDetectResponse.json'), true));
TrackingMore::shouldReceive('courier')->andReturn($courier);
$result = TrackingMore::courier()->detectCourier('XYZ124');
$result = TrackingMore::courier()->detect('XYZ124');
expect($result)->toBeArray()->not()->toBeEmpty();
expect($result['meta']['code'])->toBe(200);
;
});

it('gets all courier', function () {
$courier = mock(Courier::class);
$courier->shouldReceive('getAll')->andReturn(json_decode(File::get(__DIR__ . '/../Fixtures/allCourierResponse.json'), true));
$courier->shouldReceive('getAllCouriers')->andReturn(json_decode(File::get(__DIR__ . '/../Fixtures/allCourierResponse.json'), true));
TrackingMore::shouldReceive('courier')->andReturn($courier);
$result = TrackingMore::courier()->getAll();
$result = TrackingMore::courier()->getAllCouriers();
expect($result)->toBeArray()->not()->toBeEmpty();
expect($result['meta']['code'])->toBe(200);
expect($result['data'][0]['courier_code'])->toBe('tuffnells');
Expand Down
Loading

0 comments on commit 6184224

Please sign in to comment.