$events * @return array */ private function getEventMap(array $events): array { return \array_fill_keys( \array_map('strval', \array_unique($events)), true ); } /** * Get function events for a project, using Redis cache * @param Document|null $project * @param Database $dbForProject * @return array */ public function getFunctionsEvents(?Document $project, Database $dbForProject): array { if ($project === null || $project->isEmpty() || $project->getId() === 'console') { return []; } $hostname = $dbForProject->getAdapter()->getHostname(); $cacheKey = \sprintf( '%s-cache-%s:%s:%s:project:%s:functions:events', $dbForProject->getCacheName(), $hostname, $dbForProject->getNamespace(), $dbForProject->getTenant(), $project->getId() ); $ttl = 3600; // 1 hour cache TTL $cachedFunctionEvents = $dbForProject->getCache()->load($cacheKey, $ttl); if ($cachedFunctionEvents !== false) { $decoded = \json_decode($cachedFunctionEvents, true); return \is_array($decoded) ? $this->getEventMap(\array_keys($decoded)) : []; } $events = []; $limit = 100; $sum = 100; $offset = 0; while ($sum >= $limit) { $functions = $dbForProject->getAuthorization()->skip(fn () => $dbForProject->find('functions', [ Query::select(['$id', 'events']), Query::limit($limit), Query::offset($offset), Query::orderAsc('$sequence'), ])); $sum = \count($functions); $offset = $offset + $limit; foreach ($functions as $function) { $functionEvents = $function->getAttribute('events', []); if (!empty($functionEvents)) { \array_push($events, ...$functionEvents); } } } $uniqueEvents = $this->getEventMap($events); $dbForProject->getCache()->save($cacheKey, \json_encode($uniqueEvents)); return $uniqueEvents; } /** * Get webhook events for a project from the project's webhooks attribute * @param Document|null $project * @return array */ public function getWebhooksEvents(?Document $project): array { if ($project === null || $project->isEmpty() || $project->getId() === 'console') { return []; } $webhooks = $project->getAttribute('webhooks', []); if (empty($webhooks)) { return []; } $events = []; foreach ($webhooks as $webhook) { if ($webhook->getAttribute('enabled', false) !== true) { continue; } $webhookEvents = $webhook->getAttribute('events', []); if (!empty($webhookEvents)) { \array_push($events, ...$webhookEvents); } } return $this->getEventMap($events); } }