quote = $quote; $this->creator = $operatorId; $this->key = $this->getHash($uuid); if (array_key_exists('skip_enriching_attributes', $data)) { $this->skip_enriching_attributes = $data['skip_enriching_attributes']; } if (array_key_exists('skip_blacklist_sync', $data)) { $this->skip_blacklist_sync = $data['skip_blacklist_sync']; } $this->save(); return (int) $this->id; } public function getKeys(int $operatorId): array { $filters = [ 'creator=?', $operatorId, ]; return $this->find($filters); } public function getKey(int $operatorId): ?ApiKeys { $keys = $this->getKeys($operatorId); return $keys[0] ?? null; } public function resetKey(int $keyId, int $operatorId): void { $this->getByKeyAndOperatorId($keyId, $operatorId); if ($this->loaded()) { $uuid = sprintf('%s%s%s', $keyId, $operatorId, time()); $this->key = $this->getHash($uuid); $this->save(); } } public function getByKeyAndOperatorId(int $keyId, int $operatorId): self|null|false { $filters = [ 'id=? AND creator=?', $keyId, $operatorId, ]; return $this->load($filters); } public function getKeyIdByHash(string $hash): self|null|false { $filters = [ 'key=?', $hash, ]; return $this->load($filters); } public function getKeyById(int $keyId): self|null|false { $filters = [ 'id=?', $keyId, ]; return $this->load($filters); } public function getTimezoneByKeyId(int $keyId): string { $params = [ ':api_key' => $keyId, ]; $query = ( 'SELECT dshb_operators.timezone FROM dshb_api JOIN dshb_operators ON dshb_operators.id = dshb_api.creator WHERE dshb_api.id = :api_key' ); $results = $this->execQuery($query, $params); return $results[0]['timezone'] ?? 'UTC'; } public function getSkipEnrichingAttributes(int $keyId): array { $params = [ ':api_key' => $keyId, ]; $query = ( 'SELECT dshb_api.skip_enriching_attributes FROM dshb_api WHERE dshb_api.id = :api_key' ); $results = $this->execQuery($query, $params); if (!count($results)) { return []; } $results = json_decode($results[0]['skip_enriching_attributes']); if (!\Utils\Variables::getEmailPhoneAllowed()) { if (!in_array('email', $results, true)) { $results[] = 'email'; } if (!in_array('phone', $results, true)) { $results[] = 'phone'; } if (!in_array('domain', $results, true)) { $results[] = 'domain'; } } return $results; } public function enrichableAttributes(int $keyId): array { $skipAttributes = $this->getSkipEnrichingAttributes($keyId); $attributes = \Utils\Constants::get('ENRICHING_ATTRIBUTES'); $attributes = array_diff_key($attributes, array_flip($skipAttributes)); return $attributes; } public function attributeIsEnrichable(string $attr, int $keyId): bool { return array_key_exists($attr, $this->enrichableAttributes($keyId)); } public function getAllApiKeyIds(): array { $query = 'SELECT id from dshb_api'; return $this->execQuery($query, null); } public function updateSkipEnrichingAttributes(array $attributes): void { if ($this->loaded()) { $attributes = \array_values($attributes); $this->skip_enriching_attributes = \json_encode($attributes); $this->save(); } } public function updateSkipBlacklistSynchronisation(bool $skip): void { if ($this->loaded()) { $this->skip_blacklist_sync = $skip; $this->save(); } } public function updateRetentionPolicy(int $policyInWeeks): void { if ($this->loaded()) { $this->retention_policy = $policyInWeeks; $this->save(); } } public function updateBlacklistThreshold(int $value): void { if ($this->loaded()) { $this->blacklist_threshold = $value; $this->save(); } } public function updateReviewQueueThreshold(int $value): void { if ($this->loaded()) { $this->review_queue_threshold = $value; $this->save(); } } public function updateInternalToken(string $apiToken): void { if ($this->loaded()) { $this->token = $apiToken; $this->save(); } } }