Skip to content

Commit

Permalink
Add Workflow service and associated classes
Browse files Browse the repository at this point in the history
The update introduces a new Workflow service in the WorkflowsServiceBuilder, including supporting classes such as Batch and WorkflowInstanceItemResult. It also includes methods for workflow instances and their results. The additions provide functionality to list launched workflows and handle batch operations.

Signed-off-by: mesilov <mesilov.maxim@gmail.com>
  • Loading branch information
mesilov committed Mar 31, 2024
1 parent 7acaf7a commit bca3649
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

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

use Bitrix24\SDK\Core\Result\AbstractItem;
use Bitrix24\SDK\Services\Workflows\Common\WorkflowAutoExecutionType;
use DateTimeImmutable;
use DateTimeInterface;

/**
* @property-read string $ID workflow ID
* @property-read DateTimeImmutable $MODIFIED
* @property-read ?DateTimeImmutable $OWNED_UNTIL time for blocking of a workflow. Process is considered as unresponsive, if the difference of blocking time with the current time is more than 5 minutes;
* @property-read ?DateTimeImmutable $STARTED workflow launch date;
* @property-read ?string $MODULE_ID module ID (as per document);
* @property-read ?string $ENTITY entity ID (as per document);
* @property-read ?int $DOCUMENT_ID document ID;
* @property-read ?int $STARTED_BY who launched the workflow;
* @property-read ?int $TEMPLATE_ID workflow template ID.
*/
class WorkflowInstanceItemResult extends AbstractItem
{
public function __get($offset)
{
switch ($offset) {
case 'STARTED_BY':
case 'TEMPLATE_ID':
return (int)$this->data[$offset];
case 'DOCUMENT_ID':
if ($this->data[$offset] !== '') {
// "DEAL_158310"
return (int)substr($this->data[$offset], strpos($this->data[$offset], '_')+1);
}
return null;
case 'MODIFIED':
case 'STARTED':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}
return null;
}
return $this->data[$offset] ?? null;
}
}
26 changes: 26 additions & 0 deletions src/Services/Workflows/Workflow/Result/WorkflowInstancesResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

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

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Result\AbstractItem;
use Bitrix24\SDK\Core\Result\AbstractResult;

class WorkflowInstancesResult extends AbstractResult
{
/**
* @return WorkflowInstanceItemResult[]
* @throws BaseException
*/
public function getInstances(): array
{
$res = [];
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $item) {
$res[] = new WorkflowInstanceItemResult($item);
}

return $res;
}
}
17 changes: 17 additions & 0 deletions src/Services/Workflows/Workflow/Service/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

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

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

readonly class Batch
{
public function __construct(
protected BatchOperationsInterface $batch,
protected LoggerInterface $log)
{
}
}
56 changes: 56 additions & 0 deletions src/Services/Workflows/Workflow/Service/Workflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\Workflows\Workflow\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 Workflow extends AbstractService
{
public Batch $batch;

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

/**
* returns list of launched workflows
*
* @param array $select
* @param array $order
* @param array $filter
* @return Workflows\Workflow\Result\WorkflowInstancesResult
* @throws BaseException
* @throws TransportException
* @see https://training.bitrix24.com/rest_help/workflows/workflow/bizproc_workflow_instances.php
*/
public function instances(
array $select = ['ID', 'MODIFIED', 'OWNED_UNTIL', 'MODULE_ID', 'ENTITY', 'DOCUMENT_ID', 'STARTED', 'STARTED_BY', 'TEMPLATE_ID'],
array $order = ['STARTED' => 'DESC'],
array $filter = []): Workflows\Workflow\Result\WorkflowInstancesResult
{
return new Workflows\Workflow\Result\WorkflowInstancesResult(
$this->core->call(
'bizproc.workflow.instances',
[
'select' => $select,
'order' => $order,
'filter' => $filter,
]
)
);
}
}
15 changes: 14 additions & 1 deletion src/Services/Workflows/WorkflowsServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@ public function template(): Workflows\Template\Service\Template
{
if (!isset($this->serviceCache[__METHOD__])) {
$this->serviceCache[__METHOD__] = new Workflows\Template\Service\Template(
new Template\Service\Batch($this->batch, $this->log),
new Workflows\Template\Service\Batch($this->batch, $this->log),
$this->core,
$this->log
);
}

return $this->serviceCache[__METHOD__];
}

public function workflow(): Workflows\Workflow\Service\Workflow
{
if (!isset($this->serviceCache[__METHOD__])) {
$this->serviceCache[__METHOD__] = new Workflows\Workflow\Service\Workflow(
new Workflows\Workflow\Service\Batch($this->batch, $this->log),
$this->core,
$this->log
);
Expand Down

0 comments on commit bca3649

Please sign in to comment.