*/ public function all(): array { $stored = $this->options->get(self::OPTION, []); $connections = []; foreach (is_array($stored) ? $stored : [] as $id => $entry) { if (is_array($entry)) { $connections[(string) $id] = Connection::fromArray((string) $id, $entry); } } return $connections; } public function find(string $id): ?Connection { return $this->all()[$id] ?? null; } /** * The credentials for one Destination, or `[]` where it has no Connection. * * @return array */ public function credentialsFor(Destination $destination): array { if ($destination->connectionId === null) { return []; } $connection = $this->find($destination->connectionId); return $connection === null ? [] : $connection->credentials; } /** * @param array $credentials */ public function save(?string $id, string $type, string $label, array $credentials, ?string $accountIdentity = null): Connection { $connection = new Connection( $id !== null && $id !== '' ? $id : Ulid::generate(), $type, $label, $credentials, $accountIdentity, gmdate('c'), 'success' ); $all = $this->all(); $all[$connection->id] = $connection; $this->options->set(self::OPTION, array_map( static fn (Connection $entry): array => $entry->toArray(), $all )); return $connection; } public function delete(string $id): bool { $all = $this->all(); if (!isset($all[$id])) { return false; } unset($all[$id]); $this->options->set(self::OPTION, array_map( static fn (Connection $entry): array => $entry->toArray(), $all )); return true; } public function recordCheck(string $id, bool $success): void { $all = $this->all(); $old = $all[$id] ?? null; if ($old === null) return; $all[$id] = new Connection($old->id, $old->type, $old->label, $old->credentials, $old->accountIdentity, gmdate('c'), $success ? 'success' : 'failed'); $this->options->set(self::OPTION, array_map(static fn (Connection $entry): array => $entry->toArray(), $all)); } }