7 private const TASK_COUNTER_CODE =
'bp_tasks';
8 private const COMMENT_COUNTER_CODE =
'bp_wf_comments';
9 private const WORKFLOW_COUNTER_CODE =
'bp_workflow';
10 private readonly
int $userId;
11 private readonly
string $siteId;
12 private static array $isNeedSyncWorkflow = [];
16 $this->userId = max($userId, 0);
19 if (!isset(self::$isNeedSyncWorkflow[$this->userId]))
24 'workflow' => $workflow,
25 ] = $this->getCounters();
27 $newWorkflow = ($task ?: 0) + (
$comment ?: 0);
29 self::$isNeedSyncWorkflow[
$this->userId] = $workflow !== $newWorkflow;
40 $this->syncWorkflowIfNeed();
41 $this->increment(self::TASK_COUNTER_CODE, $increment);
42 $this->increment(self::WORKFLOW_COUNTER_CODE, $increment);
52 $this->syncWorkflowIfNeed();
53 $this->decrement(self::TASK_COUNTER_CODE, $decrement);
54 $this->decrement(self::WORKFLOW_COUNTER_CODE, $decrement);
64 $this->syncWorkflowIfNeed();
65 $this->increment(self::COMMENT_COUNTER_CODE, $increment);
66 $this->increment(self::WORKFLOW_COUNTER_CODE, $increment);
76 $this->syncWorkflowIfNeed();
77 $this->decrement(self::COMMENT_COUNTER_CODE, $decrement);
78 $this->decrement(self::WORKFLOW_COUNTER_CODE, $decrement);
81 public function setTask(
int $value = 0): void
88 $task = $this->
get(self::TASK_COUNTER_CODE);
91 $this->
set(self::TASK_COUNTER_CODE, $value);
94 $this->syncWorkflow();
104 $comment = $this->
get(self::COMMENT_COUNTER_CODE);
107 $this->
set(self::COMMENT_COUNTER_CODE, $value);
110 $this->syncWorkflow();
113 private function syncWorkflowIfNeed(): void
115 if (self::$isNeedSyncWorkflow[$this->userId])
117 $this->syncWorkflow();
122 private function syncWorkflow(): void
127 'workflow' => $workflow,
128 ] = $this->getCounters();
130 $newWorkflow = ($task ?: 0) + (
$comment ?: 0);
131 if ($newWorkflow !== $workflow)
133 $this->
set(self::WORKFLOW_COUNTER_CODE, $newWorkflow);
137 private function getCounters():
array
139 $task = $this->
get(self::TASK_COUNTER_CODE);
140 $comment = $this->
get(self::COMMENT_COUNTER_CODE);
141 $workflow = $this->
get(self::WORKFLOW_COUNTER_CODE);
146 'workflow' => $workflow
150 private function increment(
string $code,
int $increment): void
152 \CUserCounter::Increment($this->userId,
$code, $this->siteId,
true, $increment);
155 private function decrement(
string $code,
int $decrement): void
157 \CUserCounter::Decrement($this->userId,
$code, $this->siteId,
true, $decrement);
160 private function set(
string $code,
int $value): void
162 \CUserCounter::Set($this->userId,
$code, $value, $this->siteId);
167 return \CUserCounter::GetValue($this->userId,
$code, $this->siteId);