Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
SyncService.php
1<?php
2
3namespace Bitrix\Im\V2\Sync;
4
5use Bitrix\Im\Model\EO_Log_Collection;
7use Bitrix\Im\V2\Common\ContextCustomer;
11
13{
14 use ContextCustomer;
15
16 private const OFFSET_INTERVAL_IN_SECONDS = 5;
17 private const MODULE_ID = 'im';
18 private const ENABLE_OPTION_NAME = 'sync_logger_enable';
19
20 public static function isEnable(): bool
21 {
22 return Option::get(self::MODULE_ID, self::ENABLE_OPTION_NAME, 'Y') === 'Y';
23 }
24
25 public function getChangesFromDate(DateTime $date, int $limit): array
26 {
27 if (!self::isEnable())
28 {
29 return [];
30 }
31
32 $date = $this->getDateWithOffset($date);
33 $logCollection = LogTable::query()
34 ->setSelect(['ID'])
35 ->where('USER_ID', $this->getContext()->getUserId())
36 ->where('DATE_CREATE', '>=', $date)
37 ->setLimit($limit)
38 ->fetchCollection()
39 ;
40 $logCollection->fill();
41 Logger::getInstance()->updateDateDelete($logCollection);
42
43 return $this->formatData($logCollection, $limit);
44 }
45
46 public function getChangesFromId(?int $id, int $limit): array
47 {
48 if (!self::isEnable())
49 {
50 return [];
51 }
52
53 $query = LogTable::query()
54 ->setSelect(['ID'])
55 ->where('USER_ID', $this->getContext()->getUserId())
56 ->setLimit($limit)
57 ;
58
59 if ($id !== null)
60 {
61 $query->where('ID', '>', $id)->setOrder(['ID' => 'ASC']);
62 }
63 else
64 {
65 $query->setOrder(['ID' => 'DESC']);
66 }
67
68 $logCollection = $query->fetchCollection();
69 $logCollection->fill();
70 Logger::getInstance()->updateDateDelete($logCollection);
71
72 return $this->formatData($logCollection, $limit);
73 }
74
75 private function getDateWithOffset(DateTime $date): DateTime
76 {
77 $offset = self::OFFSET_INTERVAL_IN_SECONDS;
78 $date->add("- {$offset} seconds");
79
80 return $date;
81 }
82
88 private function formatData(EO_Log_Collection $logCollection, int $limit): array
89 {
90 $entities = (new EntityFactory())->createEntities(Event::initByOrmEntities($logCollection));
91 $data = [];
92
93 foreach ($entities as $entity)
94 {
95 foreach ($entity->getData() as $name => $datum)
96 {
97 $data[$name] = $datum;
98 }
99 }
100
101 $data['hasMore'] = $logCollection->count() >= $limit;
102 $ids = $logCollection->getIdList();
103 if (!empty($ids))
104 {
105 $data['lastId'] = max($ids);
106 }
107
108 return $data;
109 }
110}
static initByOrmEntities(EO_Log_Collection $logCollection)
Definition Event.php:67
getChangesFromId(?int $id, int $limit)
getChangesFromDate(DateTime $date, int $limit)
add($interval)
Definition date.php:145