Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add composite batch draft #226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/Core/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Bitrix24\SDK\Core\Response\Response;
use Generator;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;

/**
* Class Batch
Expand Down Expand Up @@ -427,7 +428,7 @@ public function getTraversableList(string $apiMethod, array $order, array $filte
/**
* @param bool $isHaltOnError
*
* @return Generator<int, ResponseData>
* @return Generator<int, ResponseData>|ResponseData[]
* @throws BaseException
* @throws Exceptions\TransportException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
Expand Down Expand Up @@ -485,10 +486,16 @@ public function getTraversable(bool $isHaltOnError): Generator
$total = $totalItems[$singleQueryKey];
}

if (Uuid::isValid($singleQueryKey)) {
$currentCommand = $this->commands->getByUuid(Uuid::fromString($singleQueryKey));
} else {
$currentCommand = $this->commands->getByName($singleQueryKey);
}
yield new ResponseData(
new Result($singleQueryResult),
Time::initFromResponse($resultQueryTimeItems[$singleQueryKey]),
new Pagination($nextItem, $total)
new Pagination($nextItem, $total),
$currentCommand
);
}
$this->log->debug('getTraversable.batchResult.processFinish');
Expand Down Expand Up @@ -555,4 +562,23 @@ private function convertToApiCommands(): array

return $apiCommands;
}

/**
* @return array
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
* @throws \Bitrix24\SDK\Core\Exceptions\TransportException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function getCompositeResultAsArray(): array
{
$result = [];
foreach ($this->getTraversable(false) as $item) {
$result[$item->getCommand()->getName() ?? $item->getCommand()->getUuid()->toString()] = $item->getResult()->getResultData();
}

return $result;
}
}
65 changes: 65 additions & 0 deletions src/Core/Commands/CommandCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Bitrix24\SDK\Core\Commands;

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Ramsey\Uuid\UuidInterface;
use SplObjectStorage;

/**
Expand All @@ -16,4 +18,67 @@
*/
class CommandCollection extends SplObjectStorage
{
private function filter(callable $filterCallback = null)
{
$filteredCollection = new static;
foreach ($this as $item) {
$itemData = null;
if ($this->offsetExists($item)) {
$itemData = $this->offsetGet($item);
}
if ($filterCallback !== null && call_user_func_array($filterCallback, [$item, $itemData]) !== true) {
continue;
}
$filteredCollection->attach($item, $itemData);
}
$filteredCollection->rewind();

return $filteredCollection;
}

/**
* @param \Ramsey\Uuid\UuidInterface $uuid
*
* @return \Bitrix24\SDK\Core\Commands\Command
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function getByUuid(UuidInterface $uuid): Command
{
$filtered = $this->filter(
static function (Command $item) use ($uuid) {
return $item->getUuid()->equals($uuid);
}
);

if ($filtered->count() === 1) {
$filtered->rewind();

return $filtered->current();
}

throw new BaseException(sprintf('command by uuid %s not found', $uuid->toString()));
}

/**
* @param string $name
*
* @return \Bitrix24\SDK\Core\Commands\Command
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function getByName(string $name): Command
{
$filtered = $this->filter(
static function (Command $item) use ($name) {
return $item->getName() === $name;
}
);

if ($filtered->count() === 1) {
$filtered->rewind();

return $filtered->current();
}

throw new BaseException(sprintf('command by name %s not found', $name));
}
}
36 changes: 20 additions & 16 deletions src/Core/Response/DTO/ResponseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,34 @@

namespace Bitrix24\SDK\Core\Response\DTO;

use Bitrix24\SDK\Core\Commands\Command;

/**
* Class ResponseData
*
* @package Bitrix24\SDK\Core\Response\DTO
*/
class ResponseData
{
/**
* @var Result
*/
protected $result;
/**
* @var Time
*/
protected $time;
/**
* @var Pagination
*/
protected $pagination;
protected Result $result;
protected Time $time;
protected Pagination $pagination;
protected Command $command;

/**
* ResponseData constructor.
*
* @param Result $result
* @param Time $time
* @param Pagination $pagination
* @param Result $result
* @param Time $time
* @param Pagination $pagination
* @param \Bitrix24\SDK\Core\Commands\Command $command
*/
public function __construct(Result $result, Time $time, Pagination $pagination)
public function __construct(Result $result, Time $time, Pagination $pagination, Command $command)
{
$this->result = $result;
$this->time = $time;
$this->pagination = $pagination;
$this->command = $command;
}

/**
Expand All @@ -61,4 +57,12 @@ public function getResult(): Result
{
return $this->result;
}

/**
* @return \Bitrix24\SDK\Core\Commands\Command
*/
public function getCommand(): Command
{
return $this->command;
}
}
5 changes: 1 addition & 4 deletions src/Core/Response/DTO/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
*/
class Result
{
/**
* @var array
*/
protected $result;
protected array $result;

/**
* Result constructor.
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function getResponseData(): DTO\ResponseData
$this->responseData = new DTO\ResponseData(
new DTO\Result($responseResult['result']),
DTO\Time::initFromResponse($responseResult['time']),
new DTO\Pagination($nextItem, $total)
new DTO\Pagination($nextItem, $total),
$this->apiCommand
);
} catch (Throwable $exception) {
$this->logger->error(
Expand Down