Skip to content

Commit

Permalink
Merge pull request #342 from KarlsonComplete/add-support-for-operatin…
Browse files Browse the repository at this point in the history
…g-timing

add support for operating timing
  • Loading branch information
mesilov committed Apr 15, 2023
2 parents 888c4a1 + a966704 commit 2fbb095
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/Services/CRM/Contact/Service/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Result\AddedItemBatchResult;
use Bitrix24\SDK\Core\Result\DeletedItemBatchResult;
use Bitrix24\SDK\Core\Result\UpdatedItemBatchResult;
use Bitrix24\SDK\Services\AbstractBatchService;
use Bitrix24\SDK\Services\CRM\Contact\Result\ContactItemResult;
use Generator;
Expand Down Expand Up @@ -224,4 +225,25 @@ public function delete(array $contactId): Generator
yield $key => new DeletedItemBatchResult($item);
}
}

/**
* Update contact
*
* Update elements in array with structure
* element_id => [ // contact id
* 'fields' => [], // contact fields to update
* 'params' => []
* ]
*
* @param array <int, array> $entityItems
*
* @return \Generator
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function update(array $entityItems): Generator
{
foreach ($this->batch->updateEntityItems('crm.contact.update', $entityItems) as $key => $item) {
yield $key => new UpdatedItemBatchResult($item);
}
}
}
59 changes: 59 additions & 0 deletions tests/Integration/OperatingTimingTest/OperatingTimingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Tests\Integration\OperatingTimingTest;

use Bitrix24\SDK\Core\Batch;
use Bitrix24\SDK\Services\CRM\Contact\Service\Contact;
use Bitrix24\SDK\Tests\Integration\Fabric;
use PHPUnit\Framework\TestCase;

/**
* Class OperatingTimingTest
*
* @package Bitrix24\SDK\Tests\Integration\OperatingTimingTest
*/
class OperatingTimingTest extends TestCase
{
protected Contact $contactService;
protected Batch $batch;

/**
* @throws \Bitrix24\SDK\Core\Exceptions\TransportException
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
*/
public function testOperatingTiming(): void
{

$timeStart = microtime(true);
$contactsToUpdate = [];
foreach ($this->contactService->batch->list([], ['>ID' => '12'], ['ID', 'PHONE'], 30000) as $contactList) {
$contactsToUpdate[$contactList->ID] = [
'fields' => [
'PHONE' => [['ID' => $contactList->PHONE[0]['ID']]]
],
'params' => [],
];
$contactListId[] = $contactList->ID;
}
foreach ($this->contactService->batch->update($contactsToUpdate) as $dealUpdateResult) {
$logOperating[] = $dealUpdateResult->getResponseData()->getTime()->getOperating();
$logOperatingResetAt = $dealUpdateResult->getResponseData()->getTime()->getOperatingResetAt();
$sumOperating = array_sum($logOperating);
echo "summa operating: " . $sumOperating . PHP_EOL;
echo "operating rest at: " . $logOperatingResetAt . PHP_EOL;
}
$timeEnd = microtime(true);
echo sprintf('batch query duration: %s seconds', round($timeEnd - $timeStart, 2)) . PHP_EOL;

self::assertGreaterThanOrEqual(5, $contactListId);

}

public function setUp(): void
{
$this->batch = Fabric::getBatchService();
$this->contactService = Fabric::getServiceBuilder()->getCRMScope()->contact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ protected function generateContacts(int $contactsCount): array
'SECOND_NAME' => sprintf('second_%s', $i),
'PHONE' => [
['VALUE' => sprintf('+7978%s', random_int(1000000, 9999999)), 'VALUE_TYPE' => 'MOBILE'],
['VALUE' => implode("-", str_split(substr( sprintf('%s', microtime()), 2, -13), 2))],
],
'EMAIL' => [
/* 'EMAIL' => [
['VALUE' => sprintf('test-%s@gmail.com', random_int(1000000, 9999999)), 'VALUE_TYPE' => 'WORK'],
],
],*/
];
}

Expand Down

0 comments on commit 2fbb095

Please sign in to comment.