<?php
declare(strict_types=1); namespace App\Application; use App\Interfaces\FileSystemInterface; use App\Exceptions\FileNotFoundException; class SettingsService { private const SETTINGS_PATH = 'meta/settings.json'; private const DEFAULTS = [ 'allow_image_paste' => false, ]; private FileSystemInterface $fileSystem; public function __construct(FileSystemInterface $fileSystem) { $this->fileSystem = $fileSystem; } public function getSettings(): array { try { $json = $this->fileSystem->read(self::SETTINGS_PATH); $data = json_decode($json, true); if (!is_array($data)) { $data = []; } } catch (FileNotFoundException $e) { $data = []; } return array_merge(self::DEFAULTS, $data); } public function updateSettings(array $data): array { $existing = $this->getSettings(); if (array_key_exists('allow_image_paste', $data)) { $existing['allow_image_paste'] = (bool)$data['allow_image_paste']; } $this->saveSettings($existing); return $existing; } private function saveSettings(array $data): void { $json = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $this->fileSystem->write(self::SETTINGS_PATH, $json); } } 