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