17 private static array $instance = [];
21 if (!array_key_exists($userId, self::$instance))
23 self::$instance[$userId] =
new self(
34 return self::$instance[$userId];
44 public function getTotal(
int $spaceId = 0): int
48 foreach ($this->providerCollection as $provider)
50 $total += $provider->getTotal($spaceId);
63 public function getValue(
int $spaceId = 0, array $metrics = []): int
72 foreach ($this->providerCollection as $provider)
74 $value += $provider->getValue($spaceId, $metrics);
86 $availableMetrics = [];
88 foreach ($this->providerCollection as $provider)
90 $availableMetrics = array_merge($availableMetrics, $provider->getAvailableMetrics());
93 return $availableMetrics;
110 $spacesTotal = $this->getMemberSpacesTotal();
111 $invitationsTotal = $this->getInvitationsTotal();
112 $total += $invitationsTotal;
113 $total += $spacesTotal[
'total'];
116 'userId' => $this->userId,
118 'spaces' => $spacesTotal[
'spaces'],
119 'invitations' => $invitationsTotal,
123 private function __construct(
private int $userId,
private ProviderCollection $providerCollection)
136 $value = $memberCounters[
'total'];
138 if (!$this->isSameValueCached($value))
158 private function isSameValueCached(
int $value): bool
160 global $CACHE_MANAGER;
162 $cache = $CACHE_MANAGER->Get(
'user_counter' . $this->userId);
168 foreach ($cache as $item)
172 && $item[
'SITE_ID'] ===
'**'
173 && (
int)$item[
'CNT'] === $value
183 private function getUserSpaces(
int $userId): array
185 $userSpaces = (
new Builder($userId))
186 ->addModeFilter(\
Bitrix\Socialnetwork\Space\List\Dictionary::FILTER_MODES[
'my'])
192 $userSpaces[] = [
'ID' => 0];
197 private function getMemberSpacesTotal(): array
202 foreach ($this->getUserSpaces($this->userId) as $space)
204 $spaceId = (int)$space[
'ID'];
205 $spaceTotal = $this->
getTotal($spaceId);
206 $total += $spaceTotal;
210 'total' => $spaceTotal,
215 $spaceCounters[
'metrics'][$metric] = $this->
getValue($spaceId, [$metric]);
218 $spaces[] = $spaceCounters;
227 private function getInvitationsTotal(): int
231 $invitations = (
new InvitationManager($this->userId))->getInvitations()->toArray();
232 foreach ($invitations as $invitation)