From be9013cad39f50eb9238eb09ce672f7ae56ae8e8 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 21 Apr 2024 19:59:01 +0600 Subject: [PATCH] Add functionality to handle Workflow Events 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 --- src/Core/Credentials/AccessToken.php | 11 +++- .../Event/Result/EventSendResult.php | 15 +++++ .../Workflows/Event/Service/Batch.php | 17 ++++++ .../Workflows/Event/Service/Event.php | 55 +++++++++++++++++++ .../Workflows/WorkflowsServiceBuilder.php | 13 +++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/Services/Workflows/Event/Result/EventSendResult.php create mode 100644 src/Services/Workflows/Event/Service/Batch.php create mode 100644 src/Services/Workflows/Event/Service/Event.php diff --git a/src/Core/Credentials/AccessToken.php b/src/Core/Credentials/AccessToken.php index 8cefe7d4..338974bf 100644 --- a/src/Core/Credentials/AccessToken.php +++ b/src/Core/Credentials/AccessToken.php @@ -6,6 +6,7 @@ use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation; /** * Class AccessToken @@ -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)) { diff --git a/src/Services/Workflows/Event/Result/EventSendResult.php b/src/Services/Workflows/Event/Result/EventSendResult.php new file mode 100644 index 00000000..c5acfee5 --- /dev/null +++ b/src/Services/Workflows/Event/Result/EventSendResult.php @@ -0,0 +1,15 @@ +getCoreResponse()->getResponseData()->getResult()[0]; + } +} \ No newline at end of file diff --git a/src/Services/Workflows/Event/Service/Batch.php b/src/Services/Workflows/Event/Service/Batch.php new file mode 100644 index 00000000..feaba616 --- /dev/null +++ b/src/Services/Workflows/Event/Service/Batch.php @@ -0,0 +1,17 @@ +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 + ] + )); + } +} \ No newline at end of file diff --git a/src/Services/Workflows/WorkflowsServiceBuilder.php b/src/Services/Workflows/WorkflowsServiceBuilder.php index 6a86b4c9..0ea134c7 100644 --- a/src/Services/Workflows/WorkflowsServiceBuilder.php +++ b/src/Services/Workflows/WorkflowsServiceBuilder.php @@ -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__])) {