Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 21 additions & 31 deletions lib/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -85,32 +75,32 @@ protected function registerCategories() {
}

/**
* @return array
* @return array<string, array{displayName: string, enabled: bool}>
*/
public function getCategories() {
public function getCategories(): array {
$this->registerCategories();

$categories = [];

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),
];
}

return $categories;
}

/**
* @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(),
Expand Down Expand Up @@ -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
);
Expand Down
47 changes: 15 additions & 32 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,45 @@

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;
use OCP\IL10N;
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()
Expand All @@ -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';
}

Expand All @@ -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;
}
}
Loading