Skip to content

Commit

Permalink
Switch from DateTimeImmutable to CarbonImmutable
Browse files Browse the repository at this point in the history
Replace all instances of DateTimeImmutable with CarbonImmutable for improved functionality and better date manipulation capabilities. Adjusted corresponding function calls and tests to align with CarbonImmutable's interface.

Signed-off-by: mesilov <[email protected]>
  • Loading branch information
mesilov committed Aug 11, 2024
1 parent 406adb6 commit c4ea91f
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/Core/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public function getTraversableList(
[
'batchCommandItemNumber' => $queryCnt,
'nextItem' => $queryResultData->getPagination()->getNextItem(),
'durationTime' => $queryResultData->getTime()->getDuration(),
'durationTime' => $queryResultData->getTime()->duration,
]
);

Expand Down Expand Up @@ -691,7 +691,7 @@ public function getTraversableListWithCount(
[
'batchCommandItemNumber' => $queryCnt,
'nextItem' => $queryResultData->getPagination()->getNextItem(),
'durationTime' => $queryResultData->getTime()->getDuration(),
'durationTime' => $queryResultData->getTime()->duration,
]
);
// iterate items in batch query result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getTraversableList(string $apiMethod, array $order, array $filte
}

$this->log->debug('FilterWithoutBatchWithoutCountOrder.step', [
'duration' => $resultPage->getResponseData()->getTime()->getDuration(),
'duration' => $resultPage->getResponseData()->getTime()->duration,
'currentElementId' => $currentElementId,
'lastElementId' => $lastElementId,
]);
Expand Down
62 changes: 11 additions & 51 deletions src/Core/Response/DTO/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,30 @@

namespace Bitrix24\SDK\Core\Response\DTO;

use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Exception;

readonly class Time
{
public function __construct(
private float $start,
private float $finish,
private float $duration,
private float $processing,
public float $start,
public float $finish,
public float $duration,
public float $processing,
/**
* @see https://training.bitrix24.com/rest_help/rest_sum/operating.php
*/
private float $operating,
private DateTimeImmutable $dateStart,
private DateTimeImmutable $dateFinish,
public float $operating,
public CarbonImmutable $dateStart,
public CarbonImmutable $dateFinish,
/**
* @see https://training.bitrix24.com/rest_help/rest_sum/operating.php
*/
private ?int $operatingResetAt
public ?int $operatingResetAt
)
{
}

public function getStart(): float
{
return $this->start;
}

public function getFinish(): float
{
return $this->finish;
}

public function getDuration(): float
{
return $this->duration;
}

public function getProcessing(): float
{
return $this->processing;
}

public function getOperating(): float
{
return $this->operating;
}

public function getOperatingResetAt(): ?int
{
return $this->operatingResetAt;
}

public function getDateStart(): DateTimeImmutable
{
return $this->dateStart;
}

public function getDateFinish(): DateTimeImmutable
{
return $this->dateFinish;
}

/**
* @throws Exception
*/
Expand All @@ -79,8 +39,8 @@ public static function initFromResponse(array $response): self
(float)$response['duration'],
(float)$response['processing'],
array_key_exists('operating', $response) ? (float)$response['operating'] : 0,
new DateTimeImmutable($response['date_start']),
new DateTimeImmutable($response['date_finish']),
new CarbonImmutable($response['date_start']),
new CarbonImmutable($response['date_finish']),
$response['operating_reset_at'] ?? null
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Services/CRM/Common/Result/AbstractCrmItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Bitrix24\SDK\Services\CRM\Common\Result\SystemFields\Types\Website;
use Bitrix24\SDK\Services\CRM\Deal\Result\DealSemanticStage;
use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException;
use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Money\Currency;
use Money\Money;

Expand All @@ -36,7 +36,7 @@ public function __construct(array $data, Currency $currency = null)
/**
* @param int|string $offset
*
* @return bool|DateTimeImmutable|int|mixed|null
* @return bool|CarbonImmutable|int|mixed|null
*/

public function __get($offset)
Expand Down Expand Up @@ -116,7 +116,7 @@ public function __get($offset)
case 'movedTime':
case 'lastActivityTime':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
return CarbonImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/Services/CRM/Deal/Result/DealCategoryItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace Bitrix24\SDK\Services\CRM\Deal\Result;

use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
use DateTimeImmutable;
use Carbon\CarbonImmutable;

/**
* Class DealItemResult
*
* @property int $ID
* @property DateTimeImmutable $CREATED_DATE
* @property CarbonImmutable $CREATED_DATE
* @property string $NAME
* @property bool $IS_LOCKED
* @property int $SORT
Expand Down
14 changes: 7 additions & 7 deletions src/Services/CRM/Item/Result/ItemItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Bitrix24\SDK\Services\CRM\Item\Result;

use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Money\Currency;

/**
Expand All @@ -15,14 +15,14 @@
* @property-read int $createdBy
* @property-read int $updatedBy
* @property-read int $movedBy
* @property-read DateTimeImmutable $createdTime
* @property-read DateTimeImmutable $updatedTime
* @property-read DateTimeImmutable $movedTime
* @property-read CarbonImmutable $createdTime
* @property-read CarbonImmutable $updatedTime
* @property-read CarbonImmutable $movedTime
* @property-read int $categoryId
* @property-read bool $opened
* @property-read string $previousStageId
* @property-read DateTimeImmutable $begindate
* @property-read DateTimeImmutable $closedate
* @property-read CarbonImmutable $begindate
* @property-read CarbonImmutable $closedate
* @property-read int $companyId
* @property-read int $contactId
* @property-read int $opportunity
Expand All @@ -38,7 +38,7 @@
* @property-read int $webformId
* @property-read int $assignedById
* @property-read int $lastActivityBy
* @property-read DateTimeImmutable $lastActivityTime
* @property-read CarbonImmutable $lastActivityTime
* @property-read string $utmSource
* @property-read string $utmMedium
* @property-read string $utmCampaign
Expand Down
7 changes: 3 additions & 4 deletions src/Services/Catalog/Common/Result/AbstractCatalogItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
use Bitrix24\SDK\Core\Result\AbstractItem;
use Bitrix24\SDK\Services\Catalog\Common\ProductType;
use Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException;
use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Money\Currency;
use Money\Money;

abstract class AbstractCatalogItem extends AbstractItem
{
Expand All @@ -31,7 +30,7 @@ public function __construct(array $data, Currency $currency = null)
/**
* @param int|string $offset
*
* @return bool|DateTimeImmutable|int|mixed|null
* @return bool|CarbonImmutable|int|mixed|null
*/

public function __get($offset)
Expand Down Expand Up @@ -72,7 +71,7 @@ public function __get($offset)
case 'dateCreate':
case 'timestampX':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
return CarbonImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}

return null;
Expand Down
14 changes: 8 additions & 6 deletions src/Services/Main/Result/ServerTimeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

namespace Bitrix24\SDK\Services\Main\Result;

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Result\AbstractResult;
use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Exception;

class ServerTimeResult extends AbstractResult
{
/**
* @return \DateTimeImmutable
* @throws \Bitrix24\SDK\Core\Exceptions\BaseException
* @throws \Exception
* @return CarbonImmutable
* @throws BaseException
* @throws Exception
*/
public function time(): DateTimeImmutable
public function time(): CarbonImmutable
{
return new DateTimeImmutable($this->getCoreResponse()->getResponseData()->getResult()[0]);
return new CarbonImmutable($this->getCoreResponse()->getResponseData()->getResult()[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Bitrix24\SDK\Services\Workflows\Common\WorkflowTaskStatusType;
use Bitrix24\SDK\Services\Workflows\Common\WorkflowTaskUserStatusType;
use Carbon\CarbonImmutable;
use DateTimeImmutable;

/**
* @property-read int $ID task ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

/**
* @property-read int $ID
Expand All @@ -19,7 +19,7 @@
* @property-read ?array $PARAMETERS
* @property-read ?array $VARIABLES
* @property-read ?array $CONSTANTS
* @property-read ?DateTimeImmutable $MODIFIED
* @property-read ?CarbonImmutable $MODIFIED
* @property-read ?bool $IS_MODIFIED
* @property-read ?int $USER_ID
* @property-read ?string $SYSTEM_CODE
Expand All @@ -39,7 +39,7 @@ public function __get($offset)
return null;
case 'MODIFIED':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
return CarbonImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}
return null;
case 'IS_MODIFIED':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

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

/**
* @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 CarbonImmutable $MODIFIED
* @property-read ?CarbonImmutable $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 ?CarbonImmutable $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;
Expand All @@ -37,7 +36,7 @@ public function __get($offset)
case 'MODIFIED':
case 'STARTED':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
return CarbonImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}
return null;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/Core/Response/DTO/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public function testInitFromResponseData(array $result): void
{
$time = Time::initFromResponse($result);

$this->assertEquals($result['start'], $time->getStart());
$this->assertEquals($result['finish'], $time->getFinish());
$this->assertEquals($result['duration'], $time->getDuration());
$this->assertEquals($result['processing'], $time->getProcessing());
$this->assertEquals($result['operating'], $time->getOperating());
$this->assertEquals($result['operating_reset_at'], $time->getOperatingResetAt());
$this->assertEquals($result['date_start'], $time->getDateStart()->format(\DATE_ATOM));
$this->assertEquals($result['date_finish'], $time->getDateFinish()->format(\DATE_ATOM));
$this->assertEquals($result['start'], $time->start);
$this->assertEquals($result['finish'], $time->finish);
$this->assertEquals($result['duration'], $time->duration);
$this->assertEquals($result['processing'], $time->processing);
$this->assertEquals($result['operating'], $time->operating);
$this->assertEquals($result['operating_reset_at'], $time->operatingResetAt);
$this->assertEquals($result['date_start'], $time->dateStart->format(\DATE_ATOM));
$this->assertEquals($result['date_finish'], $time->dateFinish->format(\DATE_ATOM));
}

public static function timingsDataProvider(): Generator
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Stubs/NullBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Bitrix24\SDK\Core\Response\DTO\Pagination;
use Bitrix24\SDK\Core\Response\DTO\ResponseData;
use Bitrix24\SDK\Core\Response\DTO\Time;
use DateTimeImmutable;
use Carbon\CarbonImmutable;
use Generator;

class NullBatch implements BatchOperationsInterface
Expand Down Expand Up @@ -41,22 +41,22 @@ public function getTraversableListWithCount(
*/
public function addEntityItems(string $apiMethod, array $entityItems): Generator
{
yield new ResponseData([],new Time(0,0,0,0,0, new DateTimeImmutable(),new DateTimeImmutable(),0,),new Pagination());
yield new ResponseData([],new Time(0,0,0,0,0, new CarbonImmutable(),new CarbonImmutable(),0,),new Pagination());
}

/**
* @inheritDoc
*/
public function deleteEntityItems(string $apiMethod, array $entityItemId): Generator
{
yield new ResponseData([],new Time(0,0,0,0,0, new DateTimeImmutable(),new DateTimeImmutable(),0,),new Pagination());
yield new ResponseData([],new Time(0,0,0,0,0, new CarbonImmutable(),new CarbonImmutable(),0,),new Pagination());
}

/**
* @inheritDoc
*/
public function updateEntityItems(string $apiMethod, array $entityItems): Generator
{
yield new ResponseData([],new Time(0,0,0,0,0, new DateTimeImmutable(),new DateTimeImmutable(),0,),new Pagination());
yield new ResponseData([],new Time(0,0,0,0,0, new CarbonImmutable(),new CarbonImmutable(),0,),new Pagination());
}
}
8 changes: 4 additions & 4 deletions tools/Commands/PerformanceBenchmarks/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ protected function simpleList(array $order, array $filter, array $select): array
);

return [
'order_count' => $default->getResponseData()->getTime()->getDuration(),
'order_without_count' => $orderAndNoCount->getResponseData()->getTime()->getDuration(),
'without_order_count' => $noOrderAndCount->getResponseData()->getTime()->getDuration(),
'without_order_without_count' => $noOrderAndNoCount->getResponseData()->getTime()->getDuration(),
'order_count' => $default->getResponseData()->getTime()->duration,
'order_without_count' => $orderAndNoCount->getResponseData()->getTime()->duration,
'without_order_count' => $noOrderAndCount->getResponseData()->getTime()->duration,
'without_order_without_count' => $noOrderAndNoCount->getResponseData()->getTime()->duration,
];
}
}

0 comments on commit c4ea91f

Please sign in to comment.