Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
calluser.php
1<?php
2
3namespace Bitrix\Im\Call;
4
8
10{
12 const STATE_UNAVAILABLE = 'unavailable';
13 const STATE_IDLE = 'idle';
14 const STATE_CALLING = 'calling';
15 const STATE_DECLINED = 'declined';
16 const STATE_BUSY = 'busy';
17 const STATE_READY = 'ready';
18
19 protected $userId;
20 protected $callId;
21 protected $state;
22 protected $lastSeen;
23 protected $firstJoined;
24 protected $isMobile;
25 protected $sharedScreen;
26 protected $recorded;
27
28 public static function create(array $fields)
29 {
30 if(!isset($fields['USER_ID']) || !$fields['USER_ID'])
31 {
32 throw new ArgumentException('USER_ID should be positive integer');
33 }
34 $instance = new static();
35 $instance->setFields($fields);
36 return $instance;
37 }
38
42 public function getState()
43 {
44 return $this->isSeenRecently() ? $this->state : static::STATE_IDLE;
45 }
46
47 public function isSeenRecently()
48 {
49 if(!($this->lastSeen instanceof DateTime))
50 {
51 return false;
52 }
53 $now = time();
54 $delta = $now - $this->lastSeen->getTimestamp();
55 return $delta <= static::LAST_SEEN_THRESHOLD;
56 }
57
58 public function updateState($state)
59 {
60 $fields = ['STATE' => $state];
61 if ($state === self::STATE_CALLING)
62 {
63 $fields['LAST_SEEN'] = new DateTime();
64 }
65 $this->update($fields);
66 }
67
71 public function getLastSeen()
72 {
73 return $this->lastSeen;
74 }
75
82 {
83 $this->update(['LAST_SEEN' => $lastSeen]);
84 }
85
86 public function getFirstJoined() : ?DateTime
87 {
88 return $this->firstJoined;
89 }
90
94 public function wasScreenShared()
95 {
97 }
98
102 public function wasRecorded()
103 {
104 return $this->recorded;
105 }
106
107
113 public function isActive()
114 {
115 $seenRecently = false;
116
117 if($this->lastSeen instanceof DateTime)
118 {
119 $now = time();
120 $delta = $now - $this->lastSeen->getTimestamp();
121 $seenRecently = $delta <= static::LAST_SEEN_THRESHOLD;
122 }
123
124 return in_array($this->state, [static::STATE_READY, static::STATE_CALLING]) && $seenRecently;
125 }
126
127 public function isUaMobile()
128 {
129 return $this->isMobile;
130 }
131
132 public function setFields(array $fields)
133 {
134 $this->userId = array_key_exists('USER_ID', $fields) ? $fields['USER_ID'] : $this->userId;
135 $this->callId = array_key_exists('CALL_ID', $fields) ? $fields['CALL_ID'] : $this->callId;
136 $this->state = array_key_exists('STATE', $fields) ? $fields['STATE'] : $this->state;
137 $this->lastSeen = array_key_exists('LAST_SEEN', $fields) ? $fields['LAST_SEEN'] : $this->lastSeen;
138 $this->firstJoined = array_key_exists('FIRST_JOINED', $fields) ? $fields['FIRST_JOINED'] : $this->firstJoined;
139 $this->isMobile = array_key_exists('IS_MOBILE', $fields) ? $fields['IS_MOBILE'] === 'Y' : $this->isMobile;
140 $this->sharedScreen = array_key_exists('SHARED_SCREEN', $fields) ? $fields['SHARED_SCREEN'] === 'Y' : $this->sharedScreen;
141 $this->recorded = array_key_exists('RECORDED', $fields) ? $fields['RECORDED'] === 'Y' : $this->recorded;
142 }
143
144 public function save()
145 {
146 CallUserTable::merge($this->toArray());
147 }
148
149 public function toArray()
150 {
151 return [
152 'USER_ID' => $this->userId,
153 'CALL_ID' => $this->callId,
154 'STATE' => $this->state,
155 'LAST_SEEN' => $this->lastSeen,
156 'FIRST_JOINED' => $this->firstJoined,
157 'IS_MOBILE' => is_bool($this->isMobile) ? $this->isMobile : null,
158 'SHARED_SCREEN' => is_bool($this->sharedScreen) ? $this->sharedScreen : null,
159 'RECORDED' => is_bool($this->recorded) ? $this->recorded : null
160 ];
161 }
162
163 public function update(array $fields)
164 {
165 $updateResult = CallUserTable::update(['CALL_ID' => $this->callId, 'USER_ID' => $this->userId], $fields);
166
167 if($updateResult->isSuccess())
168 {
169 $updateData = $updateResult->getData();
170 $this->setFields($updateData);
171 }
172 $this->setFields($fields);
173 }
174
175 public static function delete($callId, $userId)
176 {
177 CallUserTable::delete([
178 'CALL_ID' => $callId,
179 'USER_ID' => $userId
180 ]);
181 }
182}
updateLastSeen(DateTime $lastSeen)
Definition calluser.php:81
static create(array $fields)
Definition calluser.php:28
update(array $fields)
Definition calluser.php:163
setFields(array $fields)
Definition calluser.php:132