Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Base.php
1<?php
2
4
10
11abstract class Base
12{
13 abstract public function getEntityId(): string;
14 abstract protected function getOptionLimitName(): string;
15 abstract protected function getEntity(): string;
16 abstract protected function getDefaultLimit(): int;
17
18 protected Message $message;
19 protected int $counter;
20 protected int $limit = 0;
22 protected array $additionalParams = [];
23
24
25 public function __construct(Message $message)
26 {
27 $this->message = $message;
28 $this->limit = $this->initLimit();
29 }
30
31 public function setCounter(int $counter): Base
32 {
33 $this->counter = $counter;
34
35 return $this;
36 }
37
43 {
44 $this->additionalParams = $additionalParams;
45
46 return $this;
47 }
48
49 public function canUse(): bool
50 {
51 return $this->limit > 0;
52 }
53
54 public function lock(): void
55 {
56 Application::getConnection()->lock($this->getEntityId(), 60);
57 }
58
59 public function unlock(): void
60 {
61 Application::getConnection()->unlock($this->getEntityId());
62 }
63
64 public function isCanSend(): bool
65 {
66 if (isset($this->counter))
67 {
68 return $this->counter < $this->limit;
69 }
70
71 return true;
72 }
73
74 public function increase(): bool
75 {
76 if (isset($this->counter))
77 {
78 return $this->updateCounter();
79 }
80
81 $this->insertCounter();
82
83 return true;
84 }
85
86 protected function updateCounter(): bool
87 {
88 return RestrictionTable::updateCounter($this->getEntityId(), $this->limit);
89 }
90
91 protected function insertCounter(): void
92 {
93 RestrictionTable::insertCounter($this->getEntityId());
94 }
95
96 private function initLimit(): int
97 {
98 return (int)Option::get('messageservice', $this->getOptionLimitName(), $this->getDefaultLimit());
99 }
100
101 public function log()
102 {
103 if (Option::get('messageservice', 'event_log_message_send', 'N') === 'Y')
104 {
105 $restrictionType = mb_strtoupper($this->getOptionLimitName());
106 $userId = CurrentUser::get()->getId() ?: $this->message->getAuthorId();
107 $phone = $this->message->getTo();
108
109 $description = "Restriction: $restrictionType. Phone: $phone. CurrentCounter: $this->counter. Limit: $this->limit.";
110
111 \CEventLog::Log(
112 'INFO',
113 'MESSAGE_BLOCK',
114 'messageservice',
115 $userId,
116 $description
117 );
118 }
119 }
120
121}
static getConnection($name="")
setAdditionalParams(array $additionalParams)
Definition Base.php:42