Completed
Pull Request — master (#32545)
by Tom
09:55
created

TransferOwnership::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Carla Schroder <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 * @author Vincent Petry <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Files\Command;
26
27
use OCA\Files\Service\TransferOwnership\TransferOwnershipService;
28
use OCP\IUserManager;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
class TransferOwnership extends Command {
36
37
	/** @var TransferOwnershipService */
38
	protected $service;
39
	/** @var IUserManager  */
40
	protected $userManager;
41
42
	public function __construct(TransferOwnershipService $service, IUserManager $userManager) {
43
		$this->service = $service;
44
		$this->userManager = $userManager;
45
		parent::__construct();
46
	}
47
48
	protected function configure() {
49
		$this
50
			->setName('files:transfer-ownership')
51
			->setDescription('All files and folders are moved to another user - shares are moved as well.')
52
			->addArgument(
53
				'source-user',
54
				InputArgument::REQUIRED,
55
				'owner of files which shall be moved'
56
			)
57
			->addArgument(
58
				'destination-user',
59
				InputArgument::REQUIRED,
60
				'user who will be the new owner of the files'
61
			)
62
			->addOption(
63
				'path',
64
				null,
65
				InputOption::VALUE_REQUIRED,
66
				'selectively provide the path to transfer. For example --path="folder_name"'
67
			);
68
	}
69
70
	protected function execute(InputInterface $input, OutputInterface $output) {
71
		$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
72
		$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
73
		if ($sourceUserObject === null) {
74
			$output->writeln("<error>Unknown source user {$input->getArgument('source-user')}</error>");
75
			return 1;
76
		}
77
		if ($destinationUserObject === null) {
78
			$output->writeln("<error>Unknown destination user {$input->getArgument('destination-user')}</error>");
79
			return 1;
80
		}
81
82
		$inputPath = $input->getOption('path');
83
		$inputPath = \ltrim($inputPath, '/');
84
85
		try {
86
			$this->service->transfer($sourceUserObject, $destinationUserObject, $inputPath);
87
		} catch (\Exception $e) {
88
			$output->writeln('<error>And error occurred during the transfer: '.$e->getMessage().'</error>');
89
			return 1;
90
		}
91
		$output->writeln('<info>Done!</info>');
92
		return 0;
93
}
94
95
96
}
97