Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
groupqueueservice.php
1
<?php
2
3
namespace
Bitrix\Sender\Service
;
4
5
use
Bitrix\Main\ArgumentException
;
6
use
Bitrix\Main\Type\DateTime
;
7
use
Bitrix\Sender\GroupTable
;
8
use
Bitrix\Sender\Internals\Model\GroupQueueTable
;
9
use
Bitrix\Sender\Posting\Locker
;
10
11
class
GroupQueueService
implements
GroupQueueServiceInterface
12
{
13
private
const
LOCK_KEY =
'group_queue'
;
14
private
const
LIFETIME = 9800;
15
23
public
function
addToDB
(
int
$type,
int
$entityId,
int
$groupId)
24
{
25
if
(!in_array($type,
GroupQueueTable::TYPE
))
26
{
27
return
;
28
}
29
if
(!Locker::lock(self::LOCK_KEY, $entityId))
30
{
31
return
;
32
}
33
34
$current = $this->getCurrentRow($type, $entityId, $groupId);
35
if
(isset($current[
'ID'
]) || isset($current[0][
'ID'
]))
36
{
37
Locker::unlock(self::LOCK_KEY, $entityId);
38
return
;
39
}
40
41
GroupQueueTable::add([
42
'TYPE'
=> $type,
43
'ENTITY_ID'
=> $entityId,
44
'GROUP_ID'
=> $groupId,
45
]);
46
Locker::unlock(self::LOCK_KEY, $entityId);
47
}
48
56
public
function
releaseGroup
(
int
$type,
int
$entityId,
int
$groupId)
57
{
58
$current = $this->getCurrentRow($type, $entityId, $groupId)[0];
59
if
(!isset($current[
'ID'
]))
60
{
61
return
;
62
}
63
64
GroupQueueTable::delete($current[
'ID'
]);
65
66
$this->
isReleased
($groupId);
67
}
68
77
public
function
isReleased
(
int
$groupId): bool
78
{
79
$entities = GroupQueueTable::query()
80
->setSelect([
81
'ID'
,
82
'DATE_INSERT'
,
83
])
84
->where(
'GROUP_ID'
, $groupId)
85
->exec()
86
->fetchAll();
87
88
foreach
($entities as $key =>$entity)
89
{
90
$dateTime =
DateTime::createFromPhp
(
new
\
DateTime
());
91
92
if
(!$entity[
'DATE_INSERT'
] || abs($dateTime->getTimestamp() - $entity[
'DATE_INSERT'
]->getTimestamp()) > self::LIFETIME)
93
{
94
try
95
{
96
GroupQueueTable::delete($entity[
'ID'
]);
97
}
catch
(\
Exception
$e)
98
{
99
}
100
unset($entities[$key]);
101
}
102
}
103
104
if
(empty($entities))
105
{
106
GroupTable::update($groupId, [
107
'fields'
=> [
'STATUS'
=>
GroupTable::STATUS_DONE
]
108
]);
109
}
110
111
return
empty($entities);
112
}
113
114
private
function
getCurrentRow(
int
$type,
int
$entityId,
int
$groupId): array
115
{
116
return
GroupQueueTable::query()
117
->setSelect([
'ID'
])
118
->where(
'TYPE'
, $type)
119
->where(
'ENTITY_ID'
, $entityId)
120
->where(
'GROUP_ID'
, $groupId)
121
->exec()
122
->fetchAll();
123
}
124
125
public
function
isEntityProcessed
(
int
$type,
int
$entityId)
126
{
127
return
GroupQueueTable::query()
128
->setSelect([
129
'ID'
,
130
'DATE_INSERT'
,
131
])
132
->where(
'ENTITY_ID'
, $entityId)
133
->where(
'TYPE'
, $type)
134
->exec()
135
->fetch();
136
}
137
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\DB\Exception
Definition
exception.php:11
Bitrix\Main\GroupTable
Definition
group.php:29
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\Type\DateTime\createFromPhp
static createFromPhp(\DateTime $datetime)
Definition
datetime.php:232
Bitrix\Sender\GroupTable\STATUS_DONE
const STATUS_DONE
Definition
group.php:26
Bitrix\Sender\Internals\Model\GroupQueueTable
Definition
groupqueue.php:32
Bitrix\Sender\Internals\Model\GroupQueueTable\TYPE
const TYPE
Definition
groupqueue.php:33
Bitrix\Sender\Posting\Locker
Definition
locker.php:10
Bitrix\Sender\Service\GroupQueueService
Definition
groupqueueservice.php:12
Bitrix\Sender\Service\GroupQueueService\isReleased
isReleased(int $groupId)
Definition
groupqueueservice.php:77
Bitrix\Sender\Service\GroupQueueService\releaseGroup
releaseGroup(int $type, int $entityId, int $groupId)
Definition
groupqueueservice.php:56
Bitrix\Sender\Service\GroupQueueService\addToDB
addToDB(int $type, int $entityId, int $groupId)
Definition
groupqueueservice.php:23
Bitrix\Sender\Service\GroupQueueService\isEntityProcessed
isEntityProcessed(int $type, int $entityId)
Definition
groupqueueservice.php:125
Bitrix\Sender\Service\GroupQueueServiceInterface
Definition
groupqueueserviceinterface.php:6
Bitrix\Sender\Service
Definition
contactlistupdateservice.php:3
modules
sender
lib
service
groupqueueservice.php
Создано системой
1.10.0