aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/Command/SetAbsenceCommand.php
blob: bf91a163f955cb04f1114e3adea61d3922bc60c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

namespace OCA\DAV\Command;

use OCA\DAV\Service\AbsenceService;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SetAbsenceCommand extends Command {

	public function __construct(
		private IUserManager $userManager,
		private AbsenceService $absenceService,
	) {
		parent::__construct();
	}

	protected function configure(): void {
		$this->setName('dav:absence:set');
		$this->addArgument(
			'user-id',
			InputArgument::REQUIRED,
			'User ID of the affected account'
		);
		$this->addArgument(
			'first-day',
			InputArgument::REQUIRED,
			'Inclusive start day formatted as YYYY-MM-DD'
		);
		$this->addArgument(
			'last-day',
			InputArgument::REQUIRED,
			'Inclusive end day formatted as YYYY-MM-DD'
		);
		$this->addArgument(
			'short-message',
			InputArgument::REQUIRED,
			'Short message'
		);
		$this->addArgument(
			'message',
			InputArgument::REQUIRED,
			'Message'
		);
		$this->addArgument(
			'replacement-user-id',
			InputArgument::OPTIONAL,
			'Replacement user id'
		);
	}

	public function execute(InputInterface $input, OutputInterface $output): int {
		$userId = $input->getArgument('user-id');

		$user = $this->userManager->get($userId);
		if ($user === null) {
			$output->writeln('<error>User not found</error>');
			return 1;
		}

		$replacementUserId = $input->getArgument('replacement-user-id');
		if ($replacementUserId === null) {
			$replacementUser = null;
		} else {
			$replacementUser = $this->userManager->get($replacementUserId);
			if ($replacementUser === null) {
				$output->writeln('<error>Replacement user not found</error>');
				return 2;
			}
		}

		$this->absenceService->createOrUpdateAbsence(
			$user,
			$input->getArgument('first-day'),
			$input->getArgument('last-day'),
			$input->getArgument('short-message'),
			$input->getArgument('message'),
			$replacementUser?->getUID(),
			$replacementUser?->getDisplayName(),
		);

		return 0;
	}

}