Refactors dav app commands.

To improve code readability.

Signed-off-by: Faraz Samapoor <fsa@adlas.at>
This commit is contained in:
Faraz Samapoor 2023-07-05 17:45:43 +03:30 committed by Faraz Samapoor
parent 6714e51b0c
commit d13874fdf4
10 changed files with 67 additions and 203 deletions

View file

@ -32,26 +32,14 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateAddressBook extends Command {
/** @var IUserManager */
private $userManager;
/** @var CardDavBackend */
private $cardDavBackend;
/**
* @param IUserManager $userManager
* @param CardDavBackend $cardDavBackend
*/
public function __construct(IUserManager $userManager,
CardDavBackend $cardDavBackend
public function __construct(
private IUserManager $userManager,
private CardDavBackend $cardDavBackend,
) {
parent::__construct();
$this->userManager = $userManager;
$this->cardDavBackend = $cardDavBackend;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:create-addressbook')
->setDescription('Create a dav addressbook')
@ -71,6 +59,6 @@ class CreateAddressBook extends Command {
$name = $input->getArgument('name');
$this->cardDavBackend->createAddressBook("principals/users/$user", $name, []);
return 0;
return self::SUCCESS;
}
}

View file

@ -42,29 +42,15 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CreateCalendar extends Command {
/** @var IUserManager */
protected $userManager;
/** @var IGroupManager $groupManager */
private $groupManager;
/** @var \OCP\IDBConnection */
protected $dbConnection;
/**
* @param IUserManager $userManager
* @param IGroupManager $groupManager
* @param IDBConnection $dbConnection
*/
public function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection) {
public function __construct(
protected IUserManager $userManager,
private IGroupManager $groupManager,
protected IDBConnection $dbConnection,
) {
parent::__construct();
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->dbConnection = $dbConnection;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:create-calendar')
->setDescription('Create a dav calendar')
@ -110,6 +96,6 @@ class CreateCalendar extends Command {
$config
);
$caldav->createCalendar("principals/users/$user", $name, []);
return 0;
return self::SUCCESS;
}
}

View file

@ -38,40 +38,14 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DeleteCalendar extends Command {
/** @var CalDavBackend */
private $calDav;
/** @var IConfig */
private $config;
/** @var IL10N */
private $l10n;
/** @var IUserManager */
private $userManager;
/** @var LoggerInterface */
private $logger;
/**
* @param CalDavBackend $calDav
* @param IConfig $config
* @param IL10N $l10n
* @param IUserManager $userManager
*/
public function __construct(
CalDavBackend $calDav,
IConfig $config,
IL10N $l10n,
IUserManager $userManager,
LoggerInterface $logger
private CalDavBackend $calDav,
private IConfig $config,
private IL10N $l10n,
private IUserManager $userManager,
private LoggerInterface $logger,
) {
parent::__construct();
$this->calDav = $calDav;
$this->config = $config;
$this->l10n = $l10n;
$this->userManager = $userManager;
$this->logger = $logger;
}
protected function configure(): void {
@ -140,6 +114,6 @@ class DeleteCalendar extends Command {
$calendar->delete();
return 0;
return self::SUCCESS;
}
}

View file

@ -35,24 +35,14 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ListCalendars extends Command {
/** @var IUserManager */
protected $userManager;
/** @var CalDavBackend */
private $caldav;
/**
* @param IUserManager $userManager
* @param CalDavBackend $caldav
*/
public function __construct(IUserManager $userManager, CalDavBackend $caldav) {
public function __construct(
protected IUserManager $userManager,
private CalDavBackend $caldav,
) {
parent::__construct();
$this->userManager = $userManager;
$this->caldav = $caldav;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:list-calendars')
->setDescription('List all calendars of a user')
@ -100,6 +90,6 @@ class ListCalendars extends Command {
} else {
$output->writeln("<info>User <$user> has no calendars</info>");
}
return 0;
return self::SUCCESS;
}
}

View file

@ -42,37 +42,23 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class MoveCalendar extends Command {
private IUserManager $userManager;
private IGroupManager $groupManager;
private IShareManager $shareManager;
private IConfig $config;
private IL10N $l10n;
private ?SymfonyStyle $io = null;
private CalDavBackend $calDav;
private LoggerInterface $logger;
public const URI_USERS = 'principals/users/';
public function __construct(
IUserManager $userManager,
IGroupManager $groupManager,
IShareManager $shareManager,
IConfig $config,
IL10N $l10n,
CalDavBackend $calDav,
LoggerInterface $logger
private IUserManager $userManager,
private IGroupManager $groupManager,
private IShareManager $shareManager,
private IConfig $config,
private IL10N $l10n,
private CalDavBackend $calDav,
private LoggerInterface $logger,
) {
parent::__construct();
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->shareManager = $shareManager;
$this->config = $config;
$this->l10n = $l10n;
$this->calDav = $calDav;
$this->logger = $logger;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:move-calendar')
->setDescription('Move a calendar from an user to another')
@ -140,15 +126,11 @@ class MoveCalendar extends Command {
$this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination, $newName);
$this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>" . ($newName ? " as <$newName>" : ''));
return 0;
return self::SUCCESS;
}
/**
* Check if the calendar exists for user
*
* @param string $userDestination
* @param string $name
* @return bool
*/
protected function calendarExists(string $userDestination, string $name): bool {
return null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name);
@ -156,11 +138,7 @@ class MoveCalendar extends Command {
/**
* Try to find a suitable new calendar name that
* doesn't exists for the provided user
*
* @param string $userDestination
* @param string $name
* @return string
* doesn't exist for the provided user
*/
protected function getNewCalendarName(string $userDestination, string $name): string {
$increment = 1;
@ -182,10 +160,6 @@ class MoveCalendar extends Command {
/**
* Check that moving the calendar won't break shares
*
* @param array $calendar
* @param string $userOrigin
* @param string $userDestination
* @param bool $force
* @return bool had any shares or not
* @throws \InvalidArgumentException
*/

View file

@ -37,21 +37,14 @@ use Symfony\Component\Console\Output\OutputInterface;
* have no matching principal. Happened because of a bug in the calendar app.
*/
class RemoveInvalidShares extends Command {
/** @var IDBConnection */
private $connection;
/** @var Principal */
private $principalBackend;
public function __construct(IDBConnection $connection,
Principal $principalBackend) {
public function __construct(
private IDBConnection $connection,
private Principal $principalBackend,
) {
parent::__construct();
$this->connection = $connection;
$this->principalBackend = $principalBackend;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:remove-invalid-shares')
->setDescription('Remove invalid dav shares');
@ -72,13 +65,13 @@ class RemoveInvalidShares extends Command {
}
$result->closeCursor();
return 0;
return self::SUCCESS;
}
/**
* @param string $principaluri
*/
private function deleteSharesForPrincipal($principaluri) {
private function deleteSharesForPrincipal($principaluri): void {
$delete = $this->connection->getQueryBuilder();
$delete->delete('dav_shares')
->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));

View file

@ -31,18 +31,15 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RetentionCleanupCommand extends Command {
/** @var RetentionService */
private $service;
public function __construct(RetentionService $service) {
public function __construct(
private RetentionService $service,
) {
parent::__construct('dav:retention:clean-up');
$this->service = $service;
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->service->cleanUp();
return 0;
return self::SUCCESS;
}
}

View file

@ -35,22 +35,11 @@ use Symfony\Component\Console\Output\OutputInterface;
* @package OCA\DAV\Command
*/
class SendEventReminders extends Command {
/** @var ReminderService */
protected $reminderService;
/** @var IConfig */
protected $config;
/**
* @param ReminderService $reminderService
* @param IConfig $config
*/
public function __construct(ReminderService $reminderService,
IConfig $config) {
public function __construct(
protected ReminderService $reminderService,
protected IConfig $config,
) {
parent::__construct();
$this->reminderService = $reminderService;
$this->config = $config;
}
/**
@ -62,24 +51,20 @@ class SendEventReminders extends Command {
->setDescription('Sends event reminders');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($this->config->getAppValue('dav', 'sendEventReminders', 'yes') !== 'yes') {
$output->writeln('<error>Sending event reminders disabled!</error>');
$output->writeln('<info>Please run "php occ config:app:set dav sendEventReminders --value yes"');
return 1;
return self::FAILURE;
}
if ($this->config->getAppValue('dav', 'sendEventRemindersMode', 'backgroundjob') !== 'occ') {
$output->writeln('<error>Sending event reminders mode set to background-job!</error>');
$output->writeln('<info>Please run "php occ config:app:set dav sendEventRemindersMode --value occ"');
return 1;
return self::FAILURE;
}
$this->reminderService->processReminders();
return 0;
return self::SUCCESS;
}
}

View file

@ -35,30 +35,15 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncBirthdayCalendar extends Command {
/** @var BirthdayService */
private $birthdayService;
/** @var IConfig */
private $config;
/** @var IUserManager */
private $userManager;
/**
* @param IUserManager $userManager
* @param IConfig $config
* @param BirthdayService $birthdayService
*/
public function __construct(IUserManager $userManager, IConfig $config,
BirthdayService $birthdayService) {
public function __construct(
private IUserManager $userManager,
private IConfig $config,
private BirthdayService $birthdayService,
) {
parent::__construct();
$this->birthdayService = $birthdayService;
$this->config = $config;
$this->userManager = $userManager;
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:sync-birthday-calendar')
->setDescription('Synchronizes the birthday calendar')
@ -67,10 +52,6 @@ class SyncBirthdayCalendar extends Command {
'User for whom the birthday calendar will be synchronized');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->verifyEnabled();
@ -89,7 +70,7 @@ class SyncBirthdayCalendar extends Command {
$output->writeln("Start birthday calendar sync for $user");
$this->birthdayService->syncUser($user);
return 0;
return self::SUCCESS;
}
$output->writeln("Start birthday calendar sync for all users ...");
$p = new ProgressBar($output);
@ -109,10 +90,10 @@ class SyncBirthdayCalendar extends Command {
$p->finish();
$output->writeln('');
return 0;
return self::SUCCESS;
}
protected function verifyEnabled() {
protected function verifyEnabled(): void {
$isEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
if ($isEnabled !== 'yes') {

View file

@ -31,23 +31,19 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class SyncSystemAddressBook extends Command {
/**
* @param SyncService $syncService
*/
public function __construct(private SyncService $syncService, private IConfig $config) {
public function __construct(
private SyncService $syncService,
private IConfig $config,
) {
parent::__construct();
}
protected function configure() {
protected function configure(): void {
$this
->setName('dav:sync-system-addressbook')
->setDescription('Synchronizes users to the system addressbook');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$output->writeln('Syncing users ...');
$progress = new ProgressBar($output);
@ -59,6 +55,6 @@ class SyncSystemAddressBook extends Command {
$progress->finish();
$output->writeln('');
$this->config->setAppValue('dav', 'needs_system_address_book_sync', 'no');
return 0;
return self::SUCCESS;
}
}