storagePath = $storagePath; $this->logger = $logger; $this->ensureStorageDirectoryExists(); $this->loadUsers(); } public function save(User $user): void { $this->users[$user->getId()] = UserMapper::toArray($user); $this->persistUsers(); $this->logger->info("User saved: {$user->getId()}"); } public function findById(string $id): ?User { if (!isset($this->users[$id])) { return null; } try { return UserMapper::fromArray($this->users[$id]); } catch (DomainException $e) { $this->logger->error("Failed to create user from data: " . $e->getMessage()); return null; } } public function findByUsername(string $username): ?User { foreach ($this->users as $userData) { if ($userData['username'] === $username) { try { return UserMapper::fromArray($userData); } catch (DomainException $e) { $this->logger->error("Failed to create user from data: " . $e->getMessage()); return null; } } } return null; } public function exists(string $id): bool { return isset($this->users[$id]); } private function ensureStorageDirectoryExists(): void { if (!is_dir($this->storagePath)) { if (!mkdir($this->storagePath, 0775, true)) { throw new ApplicationException("Failed to create storage directory: {$this->storagePath}"); } } } private function loadUsers(): void { $filename = $this->storagePath . '/users.json'; if (!file_exists($filename)) { return; } $content = file_get_contents($filename); if ($content === false) { throw new ApplicationException("Failed to read users file: {$filename}"); } $data = json_decode($content, true); if ($data === null) { throw new ApplicationException("Failed to decode users JSON: " . json_last_error_msg()); } $this->users = $data; $this->logger->info("Loaded " . count($this->users) . " users from storage"); } private function persistUsers(): void { $filename = $this->storagePath . '/users.json'; $content = json_encode($this->users, JSON_PRETTY_PRINT); if ($content === false) { throw new ApplicationException("Failed to encode users to JSON"); } $result = file_put_contents($filename, $content); if ($result === false) { throw new ApplicationException("Failed to write users file: {$filename}"); } } }