Skip to content

Commit

Permalink
Add functionality to handle Workflow Events
Browse files Browse the repository at this point in the history
New classes have been added to handle Workflow Events, which support the initialization of the Event Service, handling Robot Requests, and sending events with return values. Modifications have also been done on the AccessToken and WorkflowsServiceBuilder classes to support this new feature.

Signed-off-by: mesilov <mesilov.maxim@gmail.com>
  • Loading branch information
mesilov committed Apr 21, 2024
1 parent 7a74bcd commit be9013c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Core/Credentials/AccessToken.php
Expand Up @@ -6,6 +6,7 @@

use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation;

/**
* Class AccessToken
Expand Down Expand Up @@ -78,10 +79,16 @@ public static function initFromArray(array $request): self
);
}

public static function initFromRobotRequest(Request $request): self
{
$requestFields = $request->request->all();
return self::initFromArray($requestFields['auth']);
}

/**
* @throws \Bitrix24\SDK\Core\Exceptions\InvalidArgumentException
* @throws InvalidArgumentException
*/
public static function initFromPlacementRequest(Request $request): self
public static function initFromPlacementRequest(HttpFoundation\Request $request): self
{
$requestFields = $request->request->all();
if (!array_key_exists('AUTH_ID', $requestFields)) {
Expand Down
15 changes: 15 additions & 0 deletions src/Services/Workflows/Event/Result/EventSendResult.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\Workflows\Event\Result;

use Bitrix24\SDK\Core\Result\AbstractResult;

class EventSendResult extends AbstractResult
{
public function isSuccess(): bool
{
return $this->getCoreResponse()->getResponseData()->getResult()[0];
}
}
17 changes: 17 additions & 0 deletions src/Services/Workflows/Event/Service/Batch.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\Workflows\Event\Service;

use Bitrix24\SDK\Core\Contracts\BatchOperationsInterface;
use Psr\Log\LoggerInterface;

readonly class Batch
{
public function __construct(
protected BatchOperationsInterface $batch,
protected LoggerInterface $log)
{
}
}
55 changes: 55 additions & 0 deletions src/Services/Workflows/Event/Service/Event.php
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\Workflows\Event\Service;

use Bitrix24\SDK\Core\Contracts\CoreInterface;
use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Exceptions\TransportException;
use Bitrix24\SDK\Services\AbstractService;
use Bitrix24\SDK\Services\Workflows;
use Psr\Log\LoggerInterface;

class Event extends AbstractService
{
public Batch $batch;

public function __construct(
Batch $batch,
CoreInterface $core,
LoggerInterface $log
)
{
parent::__construct($core, $log);
$this->batch = $batch;
}

/**
* returns output parameters to an activity. Parameters are specified in the activity description.
*
* @param string $eventToken
* @param array $returnValues
* @param string|null $logMessage
*
* @return Workflows\Event\Result\EventSendResult
* @throws BaseException
* @throws TransportException
* @see https://training.bitrix24.com/rest_help/workflows/workflows_events/bizproc_event_send.php
*/
public function send(
string $eventToken,
array $returnValues,
?string $logMessage = null,
): Workflows\Event\Result\EventSendResult
{
return new Workflows\Event\Result\EventSendResult($this->core->call(
'bizproc.event.send',
[
'event_token' => $eventToken,
'return_values' => $returnValues,
'log_message' => $logMessage
]
));
}
}
13 changes: 13 additions & 0 deletions src/Services/Workflows/WorkflowsServiceBuilder.php
Expand Up @@ -9,6 +9,19 @@

class WorkflowsServiceBuilder extends AbstractServiceBuilder
{
public function event(): Workflows\Event\Service\Event
{
if (!isset($this->serviceCache[__METHOD__])) {
$this->serviceCache[__METHOD__] = new Workflows\Event\Service\Event(
new Workflows\Event\Service\Batch($this->batch, $this->log),
$this->core,
$this->log
);
}

return $this->serviceCache[__METHOD__];
}

public function robot(): Workflows\Robot\Service\Robot
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand Down

0 comments on commit be9013c

Please sign in to comment.