Skip to content

Commit

Permalink
Merge pull request #2 from faghani/analysis-qJWnoa
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
faghani authored Mar 25, 2018
2 parents 5fbf008 + 1c744fb commit 1ce087a
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 92 deletions.
3 changes: 2 additions & 1 deletion Example/ChangeState.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once '../vendor/autoload.php';

$apiKey = 'YOUR_API_KEY';
Expand All @@ -10,4 +11,4 @@
// Returns boolean, true on success process
$res = $client->changeState($tracking_number, $state);

var_dump($res);
var_dump($res);
13 changes: 7 additions & 6 deletions Example/GetPrice.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once '../vendor/autoload.php';

$apiKey = 'YOUR_API_KEY';
Expand All @@ -7,12 +8,12 @@
// Get price
$res = $client->getPrice([
'weight' => 100, // Product weight in g
'price' => 1000, // Product price
'state' => 1, // Id state
'city' => 1, // Id city
'tip' => 0, // 0 => Sefareshi, 1 => pishtaz
'cod' => 0, // 0 => COD, 1=> Online
'price' => 1000, // Product price
'state' => 1, // Id state
'city' => 1, // Id city
'tip' => 0, // 0 => Sefareshi, 1 => pishtaz
'cod' => 0, // 0 => COD, 1=> Online
]);

// Returns array of price
var_dump($res);
var_dump($res);
25 changes: 13 additions & 12 deletions Example/NewOrder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once '../vendor/autoload.php';

$apiKey = 'YOUR_API_KEY';
Expand All @@ -7,22 +8,22 @@
// Your order array
$fakeOrder = [
'customerPhone' => '09125076324',
'reference' => '123456',
'state' => 1,
'city' => 1,
'names' => 'عروسک خرس مهربون',
'weight' => 250,
'price' => 15000,
'shipment' => 0, // Sefareshi
'payment' => 0, // COD
'customerName' => 'علیرضا فغانی',
'address' => 'پاسداران گلستان اول در قهوه ای پلاک 0',
'postalCode' => 1234567890,
'reference' => '123456',
'state' => 1,
'city' => 1,
'names' => 'عروسک خرس مهربون',
'weight' => 250,
'price' => 15000,
'shipment' => 0, // Sefareshi
'payment' => 0, // COD
'customerName' => 'علیرضا فغانی',
'address' => 'پاسداران گلستان اول در قهوه ای پلاک 0',
'postalCode' => 1234567890,
'customerEmail' => '[email protected]',
];

// Submit order
$res = $client->newOrder($fakeOrder);

// An array, $res[1] => Post tracking number
var_dump($res);
var_dump($res);
12 changes: 8 additions & 4 deletions src/Actions/ChangeState.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<?php

namespace Bdok\PostGateway\Actions;

use Bdok\PostGateway\Exceptions\ValidationException;

trait ChangeState
{
/**
* Change state of order
* Change state of order.
*
* @param $tracking_number
* @param $state
* @return boolean
*
* @throws ValidationException
*
* @return bool
*/
public function changeState($tracking_number, $state)
{
if (
!is_numeric($tracking_number) ||
!in_array((int)$state, [1,2])
!in_array((int) $state, [1, 2])
) {
throw new ValidationException([]);
}
Expand All @@ -26,4 +30,4 @@ public function changeState($tracking_number, $state)

return $result;
}
}
}
9 changes: 6 additions & 3 deletions src/Actions/GetPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
trait GetPrice
{
/**
* Get shipping price
* Get shipping price.
*
* @param array $data
* @return array
*
* @throws ValidationException
*
* @return array
*/
public function getPrice(array $data)
{
Expand All @@ -24,4 +27,4 @@ public function getPrice(array $data)

return $result;
}
}
}
28 changes: 16 additions & 12 deletions src/Actions/NewOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
trait NewOrder
{
/**
* Create new order
* Create new order.
*
* @param array $data
*
* @return array
*/
public function newOrder(array $data)
{
$dataString = $this->buildDataString($data);
$data = [
'action' => 'newOrder',
'data' => $dataString
'data' => $dataString,
];

return $this->retry(30, function () use ($data) {
Expand All @@ -28,11 +30,13 @@ public function newOrder(array $data)
}

/**
* Build Data string
* Build Data string.
*
* @param array $data
* @return string
*
* @throws ValidationException
*
* @return string
*/
protected function buildDataString(array $data)
{
Expand All @@ -42,23 +46,23 @@ protected function buildDataString(array $data)
foreach ($data as $key => $val) {
if (!isset($data[$field])) {
throw new ValidationException([]);
}
}
}

$finalString .= $data[$field] . '^';
$finalString .= $data[$field].'^';
}

$price = $this->getPrice([
'weight' => $data['weight'],
'price' => $data['price'],
'state' => $data['state'],
'city' => $data['city'],
'tip' => $data['shipment'],
'cod' => $data['payment'],
'price' => $data['price'],
'state' => $data['state'],
'city' => $data['city'],
'tip' => $data['shipment'],
'cod' => $data['payment'],
]);

$finalString .= '0^'.$price[0].'^'.$price[2]; // remember that we already ends string with ^

return trim($finalString, '^');
}
}
}
22 changes: 12 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use GuzzleHttp\Client as HttpClient;

class Client {
class Client
{
use MakesHttpRequests,
Actions\GetPrice,
Actions\NewOrder,
Expand All @@ -25,7 +26,7 @@ class Client {
public $guzzle;

/**
* Api base url
* Api base url.
*
* @var string
*/
Expand All @@ -34,22 +35,23 @@ class Client {
/**
* Create a new Client instance.
*
* @param string $apiKey
* @param \GuzzleHttp\Client $guzzle
* @param string $apiKey
* @param \GuzzleHttp\Client $guzzle
*
* @return void
*/
public function __construct($apiKey, HttpClient $guzzle = null)
{
$this->apiKey = $apiKey;

$this->guzzle = $guzzle ?: new HttpClient([
'base_uri' => $this->apiBaseUri,
'base_uri' => $this->apiBaseUri,
'http_errors' => false,
'headers' => [
'headers' => [
'Authorization' => 'Bearer '.$this->apiKey,
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
]);
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/FailedActionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public function __construct($message)
{
parent::__construct($message);
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidApiKeyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public function __construct($message)
{
parent::__construct($message);
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public function __construct()
{
parent::__construct('The resource you are looking for could not be found.');
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function getOutput()
{
return $this->output;
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function errors()
{
return $this->errors;
}
}
}
36 changes: 21 additions & 15 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

namespace Bdok\PostGateway;

use Psr\Http\Message\ResponseInterface;
use Bdok\PostGateway\Exceptions\TimeoutException;
use Bdok\PostGateway\Exceptions\NotFoundException;
use Bdok\PostGateway\Exceptions\ValidationException;
use Bdok\PostGateway\Exceptions\FailedActionException;
use Bdok\PostGateway\Exceptions\InvalidApiKeyException;
use Bdok\PostGateway\Exceptions\NotFoundException;
use Bdok\PostGateway\Exceptions\TimeoutException;
use Bdok\PostGateway\Exceptions\ValidationException;
use Psr\Http\Message\ResponseInterface;

trait MakesHttpRequests
{
/**
* Make a POST request to server and return the response.
*
* @param string $uri
* @param array $payload
* @param string $uri
* @param array $payload
*
* @return mixed
*/
private function post($uri, array $payload = [])
Expand All @@ -26,9 +27,10 @@ private function post($uri, array $payload = [])
/**
* Make request to server and return the response.
*
* @param string $verb
* @param string $uri
* @param array $payload
* @param string $verb
* @param string $uri
* @param array $payload
*
* @return mixed
*/
private function request($verb, $uri, array $payload = [])
Expand All @@ -47,13 +49,15 @@ private function request($verb, $uri, array $payload = [])
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
* @return void
* @param \Psr\Http\Message\ResponseInterface $response
*
* @throws \Exception
* @throws ValidationException
* @throws InvalidApiKeyException
* @throws NotFoundException
* @throws FailedActionException
*
* @return void
*/
private function handleRequestError(ResponseInterface $response)
{
Expand All @@ -79,10 +83,12 @@ private function handleRequestError(ResponseInterface $response)
/**
* Retry the callback or fail after x seconds.
*
* @param integer $timeout
* @param callable $callback
* @return mixed
* @param int $timeout
* @param callable $callback
*
* @throws TimeoutException
*
* @return mixed
*/
public function retry($timeout, $callback)
{
Expand All @@ -102,4 +108,4 @@ public function retry($timeout, $callback)

throw new TimeoutException($output);
}
}
}
Loading

0 comments on commit 1ce087a

Please sign in to comment.