Skip to content

Commit

Permalink
Seller/ActionsService
Browse files Browse the repository at this point in the history
  • Loading branch information
gam6itko committed Mar 4, 2020
1 parent 5b27f28 commit 5dec6b7
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 40 deletions.
86 changes: 61 additions & 25 deletions src/Service/ProductsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ public function classify(array $income)
return $this->request('POST', '/v1/product/classify', ['body' => \GuzzleHttp\json_encode($income)]);
}

/**
* @deprecated v0.2 use import
*/
public function create(array $income, bool $validateBeforeSend = true)
{
@trigger_error('Method `create` deprecated. Use `import`', E_USER_DEPRECATED);

return $this->import($income, $validateBeforeSend);
}

/**
* Creates product page in our system.
*
Expand Down Expand Up @@ -128,16 +118,6 @@ public function importBySku(array $income)
return $this->request('POST', '/v1/product/import-by-sku', ['body' => \GuzzleHttp\json_encode($income)]);
}

/**
* @deprecated since 0.2.2. use importInfo
*/
public function creationStatus(int $taskId)
{
@trigger_error(sprintf('Call deprecated method `creationStatus` use `importInfo` instead'), E_USER_DEPRECATED);

return $this->importInfo($taskId);
}

/**
* Product creation status.
*
Expand Down Expand Up @@ -197,7 +177,7 @@ public function infoBy(array $query)
*
* @return array
*/
public function stockInfo(array $pagination = [])
public function infoStocks(array $pagination = [])
{
$pagination = array_merge(
['page' => 1, 'page_size' => 100],
Expand All @@ -216,7 +196,7 @@ public function stockInfo(array $pagination = [])
*
* @return array
*/
public function pricesInfo(array $pagination = [])
public function infoPrices(array $pagination = [])
{
$pagination = array_merge(
['page' => 1, 'page_size' => 100],
Expand Down Expand Up @@ -257,7 +237,7 @@ public function list(array $filter = [], array $pagination = [])
*
* @return array
*/
public function updatePrices(array $prices)
public function importPrices(array $prices)
{
foreach ($prices as &$p) {
$p = $this->faceControl($p, ['product_id', 'offer_id', 'price', 'old_price', 'premium_price']);
Expand Down Expand Up @@ -287,7 +267,7 @@ public function updatePrices(array $prices)
*
* @return array
*/
public function updateStocks(array $stocks)
public function importStocks(array $stocks)
{
if (array_key_exists('stocks', $stocks)) {
trigger_error('You should pass stocks arg without stocks key', E_USER_NOTICE);
Expand Down Expand Up @@ -399,7 +379,7 @@ public function price(array $filter = [], array $pagination = [])
}

/**
* @see https://cb-api.ozonru.me/apiref/ru/#t-prepayment_set
* @see https://cb-api.ozonru.me/apiref/en/#t-prepayment_set
*
* @param array $data ['is_prepayment', 'offers_ids', 'products_ids']
*
Expand All @@ -411,4 +391,60 @@ public function setPrepayment(array $data)

return $this->request('POST', '/v1/product/prepayment/set', ['body' => \GuzzleHttp\json_encode($data)]);
}

//<editor-fold desc="deprecated">

/**
* @deprecated since v0.2.6 and will be deleted in v0.3. use `importPrices`.
*/
public function updatePrices(array $prices)
{
return $this->importPrices($prices);
}

/**
* @deprecated since v0.2.6 and will be deleted in v0.3. use `importStocks`.
*/
public function updateStocks(array $stocks)
{
return $this->importStocks($stocks);
}

/**
* @deprecated since v0.2.6 and will be deleted in v0.3. use `infoStocks`.
*/
public function stockInfo(array $pagination = [])
{
return $this->infoStocks($pagination);
}

/**
* @deprecated since v0.2.6 and will be deleted in v0.3. use `infoPrices`.
*/
public function pricesInfo(array $pagination = [])
{
return $this->infoPrices($pagination);
}

/**
* @deprecated since 0.2.2 and will be deleted in v0.3. use `importInfo`
*/
public function creationStatus(int $taskId)
{
@trigger_error(sprintf('Call deprecated method `creationStatus` use `importInfo` instead'), E_USER_DEPRECATED);

return $this->importInfo($taskId);
}

/**
* @deprecated v0.2 and will be deleted in v0.3. use `import`
*/
public function create(array $income, bool $validateBeforeSend = true)
{
@trigger_error('Method `create` deprecated. Use `import`', E_USER_DEPRECATED);

return $this->import($income, $validateBeforeSend);
}

//</editor-fold>
}
101 changes: 101 additions & 0 deletions src/Service/Seller/ActionsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Gam6itko\OzonSeller\Service\Seller;

use Gam6itko\OzonSeller\Service\AbstractService;

/**
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action
*
* @author Alexander Strizhak <gam6itko@gmail.com>
*/
class ActionsService extends AbstractService
{
private $path = '/sa/v1/actions';

public function __construct(int $clientId, string $apiKey, string $host = 'https://seller-api.ozon.ru/')
{
parent::__construct($clientId, $apiKey, $host);
}

/**
* Promotional offers list.
*
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action_available
*/
public function list(): array
{
return $this->request('GET', $this->path);
}

/**
* List of products which can participate in the promotional offer.
*
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action_available_products
*/
public function candidates(int $actionId, int $offset = 0, int $limit = 10): array
{
$body = [
'action_id' => $actionId,
'offset' => $offset,
'limit' => $limit,
];

return $this->request('POST', "{$this->path}/candidates", ['body' => \GuzzleHttp\json_encode($body)]);
}

/**
* List of products which participate in the promotional offer.
*
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action_products
*/
public function products(int $actionId, int $offset = 0, int $limit = 10)
{
$body = [
'action_id' => $actionId,
'offset' => $offset,
'limit' => $limit,
];

return $this->request('POST', "{$this->path}/products", ['body' => \GuzzleHttp\json_encode($body)]);
}

/**
* Add product to the promotional offer.
*
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action_add_products
*/
public function productsActivate(int $actionId, array $products): array
{
if (array_key_exists('product_id', $products)) {
$products = [$products];
}

foreach ($products as &$p) {
$p = $this->faceControl($p, ['product_id', 'action_price']);
}
unset($p);

$body = [
'action_id' => $actionId,
'products' => $products,
];

return $this->request('POST', "{$this->path}/products/activate", ['body' => \GuzzleHttp\json_encode($body)]);
}

/**
* This method allows to delete products from the promotional offer.
*
* @see https://cb-api.ozonru.me/apiref/en/#t-title_action_add_products
*/
public function productsDeactivate(int $actionId, array $productIds): array
{
$body = [
'action_id' => $actionId,
'product_ids' => $productIds,
];

return $this->request('POST', "{$this->path}/products/deactivate", ['body' => \GuzzleHttp\json_encode($body)]);
}
}
44 changes: 29 additions & 15 deletions tests/ApiReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Gam6itko\OzonSeller\Service\Posting\FbsService;
use Gam6itko\OzonSeller\Service\ProductsService;
use Gam6itko\OzonSeller\Service\ReportService;
use Gam6itko\OzonSeller\Service\Seller\ActionsService;
use PHPHtmlParser\Dom;
use PHPUnit\Framework\TestCase;

Expand All @@ -18,6 +19,10 @@
*/
class ApiReferenceTest extends TestCase
{
const IGNORE_PREFIXES = [
'/v1/order',
];

const CONFIG = [
CategoriesService::class => [
'prefix' => '/v1/category',
Expand All @@ -26,24 +31,20 @@ class ApiReferenceTest extends TestCase
],
],
ChatService::class => ['prefix' => '/v1/chat'],
OrderService::class => [
'prefix' => '/v1/order',
'mapping' => [
'123456?translit=true' => 'info',
'approve/crossborder' => 'approve',
'cancel-reason/list' => 'itemsCancelReasons',
'cancel/fbs' => 'itemsCancelFbs',
'items/cancel/crossborder' => 'itemsCancelCrossboarder',
'shipping-provider/list' => 'shippingProviders',
],
],
// OrderService::class => [
// 'prefix' => '/v1/order',
// 'mapping' => [
// '123456?translit=true' => 'info',
// 'approve/crossborder' => 'approve',
// 'cancel-reason/list' => 'itemsCancelReasons',
// 'cancel/fbs' => 'itemsCancelFbs',
// 'items/cancel/crossborder' => 'itemsCancelCrossboarder',
// 'shipping-provider/list' => 'shippingProviders',
// ],
// ],
ProductsService::class => [
'prefix' => '/v1/product',
'mapping' => [
'import/prices' => 'updatePrices', //todo rename
'import/stocks' => 'updateStocks', //todo rename
'info/prices' => 'pricesInfo', //todo rename
'info/stocks' => 'stockInfo', //todo rename
'prepayment/set' => 'setPrepayment',
],
],
Expand Down Expand Up @@ -71,6 +72,13 @@ class ApiReferenceTest extends TestCase
'cancel-reason/list' => 'cancelReasons',
],
],
//Seller
ActionsService::class => [
'prefix' => '/sa/v1/actions',
'mapping' => [
'' => 'list',
],
],
];

/**
Expand Down Expand Up @@ -107,6 +115,12 @@ public function testActualRealization()

private function isRealized($path): bool
{
foreach (self::IGNORE_PREFIXES as $ignore) {
if (0 === strpos($path, $ignore)) {
return true;
}
}

foreach (self::CONFIG as $class => $config) {
if (0 !== strpos($path, $config['prefix'])) {
continue;
Expand Down

0 comments on commit 5dec6b7

Please sign in to comment.