Skip to content

Commit

Permalink
v1.2 redis cache in auth added
Browse files Browse the repository at this point in the history
  • Loading branch information
aahmetgaliev committed Jul 24, 2019
1 parent 52812c0 commit 3e22b22
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ composer require sch-group/pickpoint
```
Примеры:

Инициализация
```
$config = [
'host' => '',
Expand All @@ -19,7 +20,23 @@ $senderDestination = new SenderDestination('Москва', 'Московская
$client = new PickPointConnector(new PickPointConf($config), $senderDestination, $defaultPackageSize);
$points = $client->getPoints(); // получить массив поставматов
Так же можно добавить кэширование, для ускорения запроса на авторизацию
$redisCacheConf = [
'host' => '127.0.0.1',
'port' => 6379
];
$client = new PickPointConnector(new PickPointConf($config), $senderDestination, $defaultPackageSize, $redisCacheConf);
```


Получение массива постаматов

```
$points = $client->getPoints(); // get postamats list
```
Цены за доставку
Expand All @@ -39,9 +56,9 @@ $invoice = new Invoice();
$invoice->setSenderCode('order: 123456');
$invoice->setPostamatNumber('5602-009');
$invoice->setDescription('Custom zakaz');
$invoice->setRecipientName('Айнур');
$invoice->setMobilePhone('+79274269594');
$invoice->setEmail('ainur_ahmetgalie@mail.ru');
$invoice->setRecipientName('Саша');
$invoice->setMobilePhone('+79274269590');
$invoice->setEmail('kek@mail.ru');
$invoice->setPostageType('unpiad'); // paid or unpaid
$invoice->setGettingType('sc'); // courier or sc
$invoice->setSum(500.00);
Expand All @@ -57,7 +74,7 @@ $product->setProductCode('1231');
$invoice->setProducts([$product]);
$address = new Address();
$address->setCityName('Казань');
$address->setPhoneNumber('+79274269594');
$address->setPhoneNumber('+79274269590');
$invoice->setClientReturnAddress($address);
$response = $client->createShipment($invoice);
```
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"require-dev": {
"predis/predis": "1.1.1",
"guzzlehttp/guzzle": "^6.2",
"piwik/ini": "^2.0",
"phpunit/phpunit": "~6.5.5"
},
Expand Down
32 changes: 26 additions & 6 deletions src/PickPoint/PickPointConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,30 @@ class PickPointConnector implements DeliveryConnector
*/
private $defaultPackageSize;

/**
* @var \Predis\Client $redisCache
*/
private $redisCache;

/**
* PickPointConnector constructor.
* @param PickPointConf $pickPointConf
* @param SenderDestination|null $senderDestination
* @param PackageSize|null $packageSize
* @param array $predisConf
*/
public function __construct(PickPointConf $pickPointConf, SenderDestination $senderDestination, PackageSize $packageSize = null)
public function __construct(
PickPointConf $pickPointConf,
SenderDestination $senderDestination,
PackageSize $packageSize = null,
array $predisConf = []
)
{
$this->client = new Client();
$this->pickPointConf = $pickPointConf;
$this->senderDestination = $senderDestination;
$this->defaultPackageSize = $packageSize;
$this->redisCache = !empty($predisConf) ? new \Predis\Client($predisConf) : null;
}

/**
Expand All @@ -60,7 +72,11 @@ public function __construct(PickPointConf $pickPointConf, SenderDestination $sen
*/
private function auth()
{
if (!empty($this->redisCache) && !empty($this->redisCache->get(self::CACHE_SESSION_KEY))) {
return $this->redisCache->get(self::CACHE_SESSION_KEY);
}
$loginUrl = $this->pickPointConf->getHost() . '/login';

try {
$request = $this->client->post($loginUrl, [
'json' => [
Expand All @@ -69,6 +85,11 @@ private function auth()
],
]);
$response = json_decode($request->getBody()->getContents(), true);

if (!empty($this->redisCache)) {
$this->redisCache->setex(self::CACHE_SESSION_KEY, self::CACHE_SESSION_LIFE_TIME, $response['SessionId']);
}

} catch (\Exception $exception) {
throw new PickPointMethodCallException($loginUrl, $exception->getMessage());
}
Expand All @@ -83,18 +104,17 @@ private function auth()
*/
public function getPoints()
{
$sessionId = $this->auth();
$postamatsUrl = $this->pickPointConf->getHost() . '/clientpostamatlist';
$url = $this->pickPointConf->getHost() . '/clientpostamatlist';

$request = $this->client->post($postamatsUrl, [
$request = $this->client->post($url, [
'json' => [
'SessionId' => $sessionId,
'SessionId' => $this->auth(),
'IKN' => $this->pickPointConf->getIKN(),
],
]);
$response = json_decode($request->getBody()->getContents(), true);

$this->checkMethodException($response, $postamatsUrl);
$this->checkMethodException($response, $url);

return $response;
}
Expand Down
17 changes: 3 additions & 14 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@

namespace PickPointSdk\Tests;

use Matomo\Ini\IniReader;
use PickPointSdk\Components\SenderDestination;
use PickPointSdk\PickPoint\PickPointConf;
use PickPointSdk\PickPoint\PickPointConnector;

class AuthTest extends InitTest
{
protected $pickPointConf;

protected $client;

public function __construct($name = null, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
$reader = new IniReader();
$config = $reader->readFile(__DIR__ . '/config.ini');
$this->pickPointConf = new PickPointConf($config);
$this->client = new PickPointConnector($this->pickPointConf, new SenderDestination($config['city_from'], $config['region_from']));
}

public function testPickPointAuthAndPostamatsGetting()
{
$postamats = $this->client->getPoints();
$this->assertTrue(is_array($postamats));
$points = $this->client->getPoints();
$this->assertTrue(is_array($points));
$this->assertNotEmpty($points[0]['Region']);
}
/**
* @expectedException \PickPointSdk\Exceptions\PickPointMethodCallException
Expand Down
2 changes: 1 addition & 1 deletion tests/CreateInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testCreateUnpaidInvoice()
$response = $this->client->createShipment($invoice);

$this->assertEquals($response['CreatedSendings'][0]['SenderCode'], $senderCode);
print_r($response);

}

public function testCreatePaidWithClientReturnAddressInvoice()
Expand Down
6 changes: 5 additions & 1 deletion tests/InitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public function __construct($name = null, array $data = array(), $dataName = '')
$this->pickPointConf = new PickPointConf($config);
$defaultPackageSize = new PackageSize(20, 20,20);
$senderDestination = new SenderDestination($config['city_from'], $config['region_from']);
$this->client = new PickPointConnector($this->pickPointConf, $senderDestination, $defaultPackageSize);
$redisConf = [
'host' => $config['redis_host'],
'port' => $config['redis_port']
];
$this->client = new PickPointConnector($this->pickPointConf, $senderDestination, $defaultPackageSize, $redisConf);
}

}
3 changes: 3 additions & 0 deletions tests/config_example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ password = apitest
ikn = 9990003041
city_from = Казань
region_from = Татарстан респ.
redis_scheme = tcp
redis_host = 127.0.0.1
redis_port = 6379

0 comments on commit 3e22b22

Please sign in to comment.