|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright 2022 Christopher Ng <chrng8@gmail.com> |
| 7 | + * |
| 8 | + * @author Christopher Ng <chrng8@gmail.com> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\DAV\Command; |
| 28 | + |
| 29 | +use OC\Core\Command\Base; |
| 30 | +use OCA\DAV\UserMigration\CalendarMigrator; |
| 31 | +use OCA\DAV\UserMigration\CalendarMigratorException; |
| 32 | +use OCP\IUser; |
| 33 | +use OCP\IUserManager; |
| 34 | +use Symfony\Component\Console\Input\InputArgument; |
| 35 | +use Symfony\Component\Console\Input\InputInterface; |
| 36 | +use Symfony\Component\Console\Output\OutputInterface; |
| 37 | + |
| 38 | +class ImportCalendar extends Base { |
| 39 | + |
| 40 | + /** @var IUserManager */ |
| 41 | + private $userManager; |
| 42 | + |
| 43 | + /** @var CalendarMigrator */ |
| 44 | + private $calendarMigrator; |
| 45 | + |
| 46 | + public function __construct( |
| 47 | + IUserManager $userManager, |
| 48 | + CalendarMigrator $calendarMigrator |
| 49 | + ) { |
| 50 | + parent::__construct(); |
| 51 | + $this->userManager = $userManager; |
| 52 | + $this->calendarMigrator = $calendarMigrator; |
| 53 | + } |
| 54 | + |
| 55 | + protected function configure() { |
| 56 | + $this |
| 57 | + ->setName('dav:import-calendar') |
| 58 | + ->setDescription('Import a calendar to a user\'s account') |
| 59 | + ->addArgument( |
| 60 | + 'user', |
| 61 | + InputArgument::REQUIRED, |
| 62 | + 'User to import the calendar for', |
| 63 | + ) |
| 64 | + ->addArgument( |
| 65 | + 'path', |
| 66 | + InputArgument::REQUIRED, |
| 67 | + 'Path to the *.ics file', |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 72 | + $user = $this->userManager->get($input->getArgument('user')); |
| 73 | + |
| 74 | + [ |
| 75 | + 'basename' => $filename, |
| 76 | + 'dirname' => $srcDir, |
| 77 | + ] = pathinfo($input->getArgument('path')); |
| 78 | + |
| 79 | + |
| 80 | + if (!$user instanceof IUser) { |
| 81 | + $output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>'); |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + $this->calendarMigrator->import($user, $srcDir, $filename, $output); |
| 87 | + } catch (CalendarMigratorException $e) { |
| 88 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 89 | + return $e->getCode() !== 0 ? $e->getCode() : 1; |
| 90 | + } |
| 91 | + |
| 92 | + return 0; |
| 93 | + } |
| 94 | +} |
0 commit comments