Skip to content

Commit

Permalink
v4/Fbs/ship (#84)
Browse files Browse the repository at this point in the history
* fix #83
  • Loading branch information
gam6itko authored Mar 26, 2024
1 parent 0276dfb commit f6d0898
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- 7.4
- 8.0
- 8.1
- 8.2
dependencies:
- highest
include:
Expand All @@ -41,7 +42,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug
ini-values: zend.assertions=1
ini-values: zend.assertions=1, assert.exception=1

- name: Install dependencies with Composer
uses: ramsey/composer-install@v1
Expand Down
6 changes: 4 additions & 2 deletions bin/is_realized.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

const SWAGGER_URL = 'https://docs.ozon.ru/api/seller/swagger.json';

$whereIsAutoloader = [
dirname(__DIR__).'/vendor/autoload.php',
dirname(__DIR__).'/autoload.php',
Expand Down Expand Up @@ -56,7 +58,7 @@
];

$client = new Client();
$response = $client->get('https://api-seller.ozon.ru/swagger.json');
$response = $client->get(SWAGGER_URL);
$json = $response->getBody()->getContents();
$swagger = json_decode($json, true);

Expand Down Expand Up @@ -93,7 +95,7 @@ function isDeprecated(string $path): bool

[$class, $method] = $arr;

$refClass = new \ReflectionClass($class);
$refClass = new ReflectionClass($class);
$refMethod = $refClass->getMethod($method);
if (!$docComment = $refMethod->getDocComment()) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions examples/v2.category.attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

$svcArgs = require_once __DIR__.'/bootstrap.php';

$svc = new \Gam6itko\OzonSeller\Service\V2\CategoryService(...$svcArgs);
$svc = new Gam6itko\OzonSeller\Service\V2\CategoryService(...$svcArgs);
$list = $svc->attribute(
17027547,
['language' => \Gam6itko\OzonSeller\Enum\Language::EN]
['language' => Gam6itko\OzonSeller\Enum\Language::EN]
);
var_dump($list);
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
</testsuite>
</testsuites>

<php>
<ini name="zend.assertions" value="1"/>
</php>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion src/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractService
/** @var StreamFactoryInterface */
protected $streamFactory;

public function __construct(array $config, ClientInterface $client, RequestFactoryInterface $requestFactory = null, StreamFactoryInterface $streamFactory = null)
public function __construct(array $config, ClientInterface $client, ?RequestFactoryInterface $requestFactory = null, ?StreamFactoryInterface $streamFactory = null)
{
$this->parseConfig($config);
$this->client = $client;
Expand Down
3 changes: 1 addition & 2 deletions src/Service/V1/CategoriesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class CategoriesService extends AbstractService
* @deprecated use V2\CategoryService::tree
* @see http://cb-api.ozonru.me/apiref/en/#t-title_get_categories_tree
*
* @param int $categoryId
* @param 'EN'|'RU' $language
*
* @return array
*/
public function tree(int $categoryId = null, string $language = 'RU')
public function tree(?int $categoryId = null, string $language = 'RU')
{
$query = array_filter([
'category_id' => $categoryId,
Expand Down
1 change: 0 additions & 1 deletion src/Service/V1/FinanceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FinanceService extends AbstractService
*/
public function realization(array $query)
{

$query = ArrayHelper::pick($query, ['date']);

return $this->request('POST', '/v1/finance/realization', $query);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/V1/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public function deactivate(int $productId): bool
*
* @deprecated
*/
public function delete(int $productId, string $offerId = null)
public function delete(int $productId, ?string $offerId = null)
{
$query = array_filter([
'product_id' => $productId,
Expand Down
4 changes: 2 additions & 2 deletions src/Service/V1/ReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function list(array $query)
*
* @return array|string
*/
public function info(string $code = null)
public function info(?string $code = null)
{
$query = array_filter(['code' => $code]);

Expand Down Expand Up @@ -69,7 +69,7 @@ public function products(array $query = [])
*
* @return array|string
*/
public function transaction(\DateTimeInterface $dateFrom, \DateTimeInterface $dateTo, string $search = null, string $transactionType = TransactionType::ALL)
public function transaction(\DateTimeInterface $dateFrom, \DateTimeInterface $dateTo, ?string $search = null, string $transactionType = TransactionType::ALL)
{
$query = array_filter([
'date_from' => $dateFrom->format('Y-m-d'),
Expand Down
2 changes: 1 addition & 1 deletion src/Service/V2/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function attributeValueByOption(string $language = 'RU', array $options =
/**
* @see https://api-seller.ozon.ru/v2/category/tree
*/
public function tree(int $categoryId = null, string $language = 'DEFAULT')
public function tree(?int $categoryId = null, string $language = 'DEFAULT')
{
$body = [
'language' => $language,
Expand Down
4 changes: 2 additions & 2 deletions src/Service/V2/Posting/FbsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function arbitration($postingNumber): bool
/**
* @see https://cb-api.ozonru.me/apiref/en/#t-fbs_cancel_title
*/
public function cancel(string $postingNumber, int $cancelReasonId, string $cancelReasonMessage = null): bool
public function cancel(string $postingNumber, int $cancelReasonId, ?string $cancelReasonMessage = null): bool
{
$body = [
'posting_number' => $postingNumber,
Expand Down Expand Up @@ -318,7 +318,7 @@ public function setTrackingNumber(array $trackingNumbers): array
public function digitalActGetPdf(int $id, string $docType): array
{
return $this->request('POST', "{$this->path}/digital/act/get-pdf", [
'id' => $id,
'id' => $id,
'doc_type' => $docType,
]);
}
Expand Down
91 changes: 91 additions & 0 deletions src/Service/V4/Posting/FbsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Gam6itko\OzonSeller\Service\V4\Posting;

use Gam6itko\OzonSeller\Enum\PostingScheme;
use Gam6itko\OzonSeller\Service\AbstractService;
use Gam6itko\OzonSeller\Utils\ArrayHelper;
use Gam6itko\OzonSeller\Utils\WithResolver;

/**
* @psalm-type TShipResponseProduct = array{
* mandatory_mark: list<string>,
* name: string,
* offer_id: string,
* price: string,
* quantity: integer,
* sku: integer,
* currency_code: string
* }
* @psalm-type TShipResponseAdditionalData = array{
* posting_number: string,
* products: list<TShipResponseProduct>
* }
* @psalm-type TShipResponse = array{
* additional_data: TShipResponseAdditionalData,
* result: list<string>
* }
* @psalm-type THasProducts = array{
* products: list<TShipProduct>
* }
* @psalm-type TShipProduct = array{
* product_id: int,
* quantity: int
* }
* @psalm-type TShipPackageProduct = array{
* exemplarsIds: int,
* product_id: int
* quantity: int
* }
*
* @psalm-type TShipWith = array{additional_data: bool}
*/
class FbsService extends AbstractService
{
private $path = '/v4/posting/fbs';

/**
* @see https://docs.ozon.ru/api/seller/#operation/PostingAPI_ShipFbsPostingV4
*
* @param $packages list<THasProducts>
* @param $with TShipWith
*
* @return TShipResponse
*/
public function ship(array $packages, string $postingNumber, array $with = []): array
{
\assert([] !== $packages);
\assert(!$this->isAssoc($packages));
foreach ($packages as &$package) {
\assert(\array_key_exists('products', $package));
$package = ArrayHelper::pick($package, ['products']);
\assert(!$this->isAssoc($package['products']));
\assert(\count($package['products']) > 0);
}

$body = [
'packages' => $packages,
'posting_number' => $postingNumber,
'with' => WithResolver::resolve($with, 4, PostingScheme::FBS, __FUNCTION__),
];

return $this->request('POST', "$this->path/ship", $body);
}

/**
* @see https://docs.ozon.ru/api/seller/#operation/PostingAPI_ShipFbsPostingPackage
*
* @return TShipResponse
*/
public function shipPackage(string $postingNumber, array $products): array
{
$body = [
'posting_number' => $postingNumber,
'products' => $products,
];

return $this->request('POST', "$this->path/ship/package", $body);
}
}
23 changes: 23 additions & 0 deletions src/Service/V4/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,27 @@ public function infoPrices(array $filter, ?string $lastId = '', int $limit = 100

return $this->request('POST', "{$this->path}/info/prices", $body);
}

/**
* @see https://docs.ozon.ru/api/seller/#operation/ProductAPI_GetUploadQuota
*
* @psalm-type TQuota = array{
* limit: int,
* reset_at: string,
* usage: int
* }
*
* @return array{
* daily_create: TQuota,
* daily_update: TQuota,
* total: array{
* limit: int,
* usage: int,
* },
* }
*/
public function infoLimit(): array
{
return $this->request('POST', "{$this->path}/info/limit", '{}');
}
}
4 changes: 4 additions & 0 deletions src/Utils/WithResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

final class WithResolver
{
/**
* @psalm-suppress TypeDoesNotContainType
*/
public static function getKeys(int $version = 2, string $postingScheme = 'fbs', ?string $method = ''): array
{
$arr = array_filter([$version, $postingScheme, $method]);
Expand All @@ -17,6 +20,7 @@ public static function getKeys(int $version = 2, string $postingScheme = 'fbs',
case [2, PostingScheme::FBO]:
return ['analytics_data', 'financial_data'];
case [3, PostingScheme::FBS, 'ship']:
case [4, PostingScheme::FBS, 'ship']:
return ['additional_data'];
default:
return ['analytics_data', 'barcodes', 'financial_data'];
Expand Down
2 changes: 1 addition & 1 deletion tests/Service/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function createSvc(ClientInterface $client, RequestFactoryInterface $r
);
}

protected function quickTest(string $methodName, array $arguments, array $expectedRequest, string $responseJson = '{"result": []}', callable $fnPostRequest = null)
protected function quickTest(string $methodName, array $arguments, array $expectedRequest, string $responseJson = '{"result": []}', ?callable $fnPostRequest = null)
{
[$method, $path, $expectedOptions] = $expectedRequest;
$client = $this->createClient($method, $path, $expectedOptions, $responseJson);
Expand Down
Loading

0 comments on commit f6d0898

Please sign in to comment.