diff --git a/lib/Collector.php b/lib/Collector.php index 0f47838..f03d319 100644 --- a/lib/Collector.php +++ b/lib/Collector.php @@ -18,6 +18,8 @@ use OCA\Survey_Client\Categories\Stats; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Services\IAppConfig; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IDBConnection; @@ -27,32 +29,20 @@ class Collector { public const SURVEY_SERVER_URL = 'https://surveyserver.nextcloud.com/'; /** @var ICategory[] */ - protected $categories; - - /** @var IClientService */ - protected $clientService; - - /** @var IConfig */ - protected $config; - - /** @var IDBConnection */ - protected $connection; - - /** @var IniGetWrapper */ - protected $phpIni; - - /** @var IL10N */ - protected $l; - - public function __construct(IClientService $clientService, IConfig $config, IDBConnection $connection, IniGetWrapper $phpIni, IL10N $l) { - $this->clientService = $clientService; - $this->config = $config; - $this->connection = $connection; - $this->phpIni = $phpIni; - $this->l = $l; + protected array $categories; + + public function __construct( + protected IClientService $clientService, + protected IConfig $config, + protected IAppConfig $appConfig, + protected IDBConnection $connection, + protected IniGetWrapper $phpIni, + protected IL10N $l, + protected ITimeFactory $timeFactory, + ) { } - protected function registerCategories() { + protected function registerCategories(): void { $this->categories[] = new Server( $this->config, $this->l @@ -85,9 +75,9 @@ protected function registerCategories() { } /** - * @return array + * @return array */ - public function getCategories() { + public function getCategories(): array { $this->registerCategories(); $categories = []; @@ -95,7 +85,7 @@ public function getCategories() { foreach ($this->categories as $category) { $categories[$category->getCategory()] = [ 'displayName' => $category->getDisplayName(), - 'enabled' => $this->config->getAppValue('survey_client', $category->getCategory(), 'yes') === 'yes', + 'enabled' => $this->appConfig->getAppValueBool($category->getCategory(), true), ]; } @@ -103,14 +93,14 @@ public function getCategories() { } /** - * @return array + * @return array{id: string, items: array} */ public function getReport() { $this->registerCategories(); $tuples = []; foreach ($this->categories as $category) { - if ($this->config->getAppValue('survey_client', $category->getCategory(), 'yes') === 'yes') { + if ($this->appConfig->getAppValueBool($category->getCategory(), true)) { foreach ($category->getData() as $key => $value) { $tuples[] = [ $category->getCategory(), @@ -150,8 +140,8 @@ public function sendReport(): DataResponse { } if ($response->getStatusCode() === Http::STATUS_OK) { - $this->config->setAppValue('survey_client', 'last_sent', (string)time()); - $this->config->setAppValue('survey_client', 'last_report', json_encode($report)); + $this->appConfig->setAppValueInt('last_sent', $this->timeFactory->getTime()); + $this->appConfig->setAppValueString('last_report', json_encode($report), true); return new DataResponse( $report ); diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 6e314ab..19952f1 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -7,8 +7,10 @@ namespace OCA\Survey_Client\Settings; +use OCA\Survey_Client\BackgroundJobs\MonthlyReport; use OCA\Survey_Client\Collector; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IAppConfig; use OCP\BackgroundJob\IJobList; use OCP\IConfig; use OCP\IDateTimeFormatter; @@ -16,53 +18,34 @@ use OCP\Settings\ISettings; class AdminSettings implements ISettings { - - /** @var Collector */ - private $collector; - - /** @var IConfig */ - private $config; - - /** @var IL10N */ - private $l; - - /** @var IDateTimeFormatter */ - private $dateTimeFormatter; - - /** @var IJobList */ - private $jobList; - - public function __construct(Collector $collector, - IConfig $config, - IL10N $l, - IDateTimeFormatter $dateTimeFormatter, - IJobList $jobList, + public function __construct( + protected Collector $collector, + protected IConfig $config, + protected IAppConfig $appConfig, + protected IL10N $l, + protected IDateTimeFormatter $dateTimeFormatter, + protected IJobList $jobList, ) { - $this->collector = $collector; - $this->config = $config; - $this->l = $l; - $this->dateTimeFormatter = $dateTimeFormatter; - $this->jobList = $jobList; } /** * @return TemplateResponse */ - public function getForm() { - $lastSentReportTime = (int)$this->config->getAppValue('survey_client', 'last_sent', '0'); + public function getForm(): TemplateResponse { + $lastSentReportTime = $this->appConfig->getAppValueInt('last_sent'); if ($lastSentReportTime === 0) { $lastSentReportDate = $this->l->t('Never'); } else { $lastSentReportDate = $this->dateTimeFormatter->formatDate($lastSentReportTime); } - $lastReport = $this->config->getAppValue('survey_client', 'last_report', ''); + $lastReport = $this->appConfig->getAppValueString('last_report', lazy: true); if ($lastReport !== '') { $lastReport = json_encode(json_decode($lastReport, true), JSON_PRETTY_PRINT); } $parameters = [ - 'is_enabled' => $this->jobList->has('OCA\Survey_Client\BackgroundJobs\MonthlyReport', null), + 'is_enabled' => $this->jobList->has(MonthlyReport::class, null), 'last_sent' => $lastSentReportDate, 'last_report' => $lastReport, 'categories' => $this->collector->getCategories() @@ -74,7 +57,7 @@ public function getForm() { /** * @return string the section ID, e.g. 'sharing' */ - public function getSection() { + public function getSection(): string { return 'survey_client'; } @@ -83,7 +66,7 @@ public function getSection() { * the admin section. The forms are arranged in ascending order of the * priority values. It is required to return a value between 0 and 100. */ - public function getPriority() { + public function getPriority(): int { return 50; } }