Skip to content
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ composer.lock
tools/.env.local
tools/logs
tests/ApplicationBridge/auth.json
tests/ApplicationBridge/*.log
/examples/*.log
/examples/**/*.log
/examples/**/*/*.log
Expand Down
4 changes: 1 addition & 3 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

$finder = Finder::create()
->in(__DIR__ . '/src/Infrastructure/Console/Commands/')
->in(__DIR__ . '/src/Services/CRM/VatRates/')
->in(__DIR__ . '/src/Services/CRM/Contact/')
->in(__DIR__ . '/src/Services/CRM/Requisites/')
->in(__DIR__ . '/src/Services/CRM/Automation/')
->name('*.php')
->exclude(['vendor', 'storage', 'docker', 'docs']) // Exclude directories
->ignoreDotFiles(true)
Expand Down
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

- Fixed error in arguments in service for method `placement.bind`, [see details](https://github.com/bitrix24/b24phpsdk/issues/151)

## 1.3.0 – 2025.04.23

### Added

- Added service `Services\CRM\Automation\Service\Trigger` with support methods,
see [add crm.automation.trigger* methods](https://github.com/bitrix24/b24phpsdk/issues/148):
- `add` add new trigger, with batch calls support
- `delete` delete trigger, with batch calls support
- `list` get list of triggers, with batch calls support
- `execute` execute trigger, with batch calls support
- Added **PHP 8.4** [support](https://github.com/bitrix24/b24phpsdk/issues/120) 🚀
- Added method `Bitrix24\SDK\Services\Main\Service::guardValidateCurrentAuthToken` for validate current auth token with
api-call `app.info` on vendor OAUTH server.
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ test-integration-scope-entity:
.PHONY: test-integration-scope-ai-admin
test-integration-scope-ai-admin:
docker-compose run --rm php-cli vendor/bin/phpunit --testsuite integration_tests_scope_ai_admin

.PHONY: integration_tests_scope_automation
integration_tests_scope_automation:
docker-compose run --rm php-cli vendor/bin/phpunit --testsuite integration_tests_scope_automation

# work dev environment
.PHONY: php-dev-server-up
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
- tests/Integration/Services/IMOpenLines
- tests/Integration/Services/Main
- tests/Integration/Services/Placement
- tests/Integration/Services/CRM/Automation
bootstrapFiles:
- tests/bootstrap.php
parallel:
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<testsuite name="integration_tests_scope_ai_admin">
<directory>./tests/Integration/Services/AI/</directory>
</testsuite>
<testsuite name="integration_tests_scope_automation">
<directory>./tests/Integration/Services/CRM/Automation/</directory>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
__DIR__ . '/tests/Integration/Services/IM',
__DIR__ . '/src/Services/IMOpenLines',
__DIR__ . '/tests/Integration/Services/IMOpenLines',
__DIR__ . '/src/Services/CRM/Automation',
__DIR__ . '/tests/Integration/Services/CRM/Automation',
__DIR__ . '/src/Services/Main',
__DIR__ . '/tests/Integration/Services/Main',
__DIR__ . '/src/Services/Placement',
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Batch implements BatchOperationsInterface
/**
* Batch constructor.
*/
public function __construct(private readonly CoreInterface $core, private readonly LoggerInterface $logger)
public function __construct(protected readonly CoreInterface $core, protected readonly LoggerInterface $logger)
{
$this->commands = new CommandCollection();
}
Expand Down Expand Up @@ -345,9 +345,9 @@ protected function getReverseOrder(array $order): array
*/
public function getTraversableList(
string $apiMethod,
array $order,
array $filter,
array $select,
?array $order = [],
?array $filter = [],
?array $select = [],
?int $limit = null,
?array $additionalParameters = null
): Generator {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Contracts/BatchOperationsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function addEntityItems(string $apiMethod, array $entityItems): Generator
/**
* Delete entity items with batch call
*
* @param array<int, int> $entityItemId
* @param array<int, mixed> $entityItemId
*
* @return Generator<int, ResponseData>|ResponseData[]
* @throws BaseException
Expand Down
164 changes: 164 additions & 0 deletions src/Services/CRM/Automation/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

/**
* This file is part of the bitrix24-php-sdk package.
*
* © Vadim Soluyanov <vadimsallee@gmail.com>
*
* For the full copyright and license information, please view the MIT-LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Automation;

use Bitrix24\SDK\Core\Commands\Command;
use Bitrix24\SDK\Core\Commands\CommandCollection;
use Bitrix24\SDK\Core\Contracts\CoreInterface;
use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Exceptions\InvalidArgumentException;
use Bitrix24\SDK\Core\Response\DTO\Pagination;
use Bitrix24\SDK\Core\Response\DTO\ResponseData;
use Bitrix24\SDK\Core\Response\DTO\Result;
use Bitrix24\SDK\Core\Response\DTO\Time;
use Bitrix24\SDK\Core\Response\Response;
use Generator;
use Psr\Log\LoggerInterface;

/**
* Class Batch
*
* @package Bitrix24\SDK\Services\CRM\Automation
*/
class Batch extends \Bitrix24\SDK\Core\Batch
{
/**
* Delete entity items with batch call
*
*
* @return Generator<int, ResponseData>|ResponseData[]
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function deleteEntityItems(
string $apiMethod,
array $entityItemId,
?array $additionalParameters = null
): Generator {
$this->logger->debug(
'deleteEntityItems.start',
[
'apiMethod' => $apiMethod,
'entityItems' => $entityItemId,
'additionalParameters' => $additionalParameters,
]
);

try {
$this->clearCommands();
foreach ($entityItemId as $cnt => $code) {
if (!is_string($code)) {
throw new InvalidArgumentException(
sprintf(
'invalid type «%s» of trigger code «%s» at position %s, code must be string type',
gettype($code),
$code,
$cnt
)
);
}

$parameters = ['CODE' => $code];
$this->registerCommand($apiMethod, $parameters);
}

foreach ($this->getTraversable(true) as $cnt => $deletedItemResult) {
yield $cnt => $deletedItemResult;
}
} catch (InvalidArgumentException $exception) {
$errorMessage = sprintf('batch delete entity items: %s', $exception->getMessage());
$this->logger->error(
$errorMessage,
[
'trace' => $exception->getTrace(),
]
);
throw $exception;
} catch (\Throwable $exception) {
$errorMessage = sprintf('batch delete entity items: %s', $exception->getMessage());
$this->logger->error(
$errorMessage,
[
'trace' => $exception->getTrace(),
]
);

throw new BaseException($errorMessage, $exception->getCode(), $exception);
}

$this->logger->debug('deleteEntityItems.finish');
}

/**
* Execute triggers with batch call
*
*
* @return Generator<int, ResponseData>|ResponseData[]
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function executeItems(
string $apiMethod,
array $triggers
): Generator {
$this->logger->debug(
'executeItems.start',
[
'apiMethod' => $apiMethod,
'triggers' => $triggers,
]
);

try {
$this->clearCommands();
foreach ($triggers as $cnt => $fields) {
if (!is_array($fields)) {
throw new InvalidArgumentException(
sprintf(
'invalid type «%s» of trigger params «%s» at position %s, params must be array type',
gettype($fields),
$fields,
$cnt
)
);
}

$this->registerCommand($apiMethod, $fields);
}

foreach ($this->getTraversable(true) as $cnt => $executeResult) {
yield $cnt => $executeResult;
}
} catch (InvalidArgumentException $exception) {
$errorMessage = sprintf('batch execute triggers: %s', $exception->getMessage());
$this->logger->error(
$errorMessage,
[
'trace' => $exception->getTrace(),
]
);
throw $exception;
} catch (\Throwable $exception) {
$errorMessage = sprintf('batch execute triggers: %s', $exception->getMessage());
$this->logger->error(
$errorMessage,
[
'trace' => $exception->getTrace(),
]
);

throw new BaseException($errorMessage, $exception->getCode(), $exception);
}

$this->logger->debug('executeItems.finish');
}
}
26 changes: 26 additions & 0 deletions src/Services/CRM/Automation/Result/TriggerItemResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* This file is part of the bitrix24-php-sdk package.
*
* © Vadim Soluyanov <vadimsallee@gmail.com>
*
* For the full copyright and license information, please view the MIT-LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Automation\Result;

use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;

/**
* Class TriggerItemResult
*
* @property-read string $CODE
* @property-read string|null $NAME
*/
class TriggerItemResult extends AbstractCrmItem
{
}
33 changes: 33 additions & 0 deletions src/Services/CRM/Automation/Result/TriggerResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* This file is part of the bitrix24-php-sdk package.
*
* © Vadim Soluyanov <vadimsallee@gmail.com>
*
* For the full copyright and license information, please view the MIT-LICENSE.txt
* file that was distributed with this source code.
*/


declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Automation\Result;

use Bitrix24\SDK\Core\Result\AbstractResult;

/**
* Class TriggerResult
*
* @package Bitrix24\SDK\Services\CRM\Automation\Result
*/
class TriggerResult extends AbstractResult
{
/**
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function trigger(): TriggerItemResult
{
return new TriggerItemResult($this->getCoreResponse()->getResponseData()->getResult());
}
}
48 changes: 48 additions & 0 deletions src/Services/CRM/Automation/Result/TriggersResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the bitrix24-php-sdk package.
*
* © Vadim Soluyanov <vadimsallee@gmail.com>
*
* For the full copyright and license information, please view the MIT-LICENSE.txt
* file that was distributed with this source code.
*/


declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Automation\Result;

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

/**
* Class TriggersResult
*
* @package Bitrix24\SDK\Services\CRM\Automation\Result
*/
class TriggersResult extends AbstractResult
{
/**
* @return TriggerItemResult[]
* @throws BaseException
*/
public function getTriggers(): array
{
$items = [];
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $item) {
$items[] = new TriggerItemResult($item);
}

return $items;
}

/**
* @throws BaseException
*/
public function getTriggersArray(): array
{
return $this->getCoreResponse()->getResponseData()->getResult();
}
}
Loading