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

TransferOwnership   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 21 1
A execute() 0 24 4
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 OC\Encryption\Manager;
28
use OC\Files\Filesystem;
29
use OC\Files\View;
30
use OC\Share20\ProviderFactory;
31
use OCA\Files\Service\TransferOwnership\TransferOwnershipService;
32
use OCP\Files\FileInfo;
33
use OCP\Files\Mount\IMountManager;
34
use OCP\ILogger;
35
use OCP\IUserManager;
36
use OCP\Share\IManager;
37
use OCP\Share\IShare;
38
use Symfony\Component\Console\Command\Command;
39
use Symfony\Component\Console\Helper\ProgressBar;
40
use Symfony\Component\Console\Input\InputArgument;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Input\InputOption;
43
use Symfony\Component\Console\Output\OutputInterface;
44
45
class TransferOwnership extends Command {
46
47
	/** @var TransferOwnershipService */
48
	protected $service;
49
	/** @var IUserManager  */
50
	protected $userManager;
51
52
	public function __construct(TransferOwnershipService $service, IUserManager $userManager) {
53
		$this->service = $service;
54
		$this->userManager = $userManager;
55
		parent::__construct();
56
	}
57
58
	protected function configure() {
59
		$this
60
			->setName('files:transfer-ownership')
61
			->setDescription('All files and folders are moved to another user - shares are moved as well.')
62
			->addArgument(
63
				'source-user',
64
				InputArgument::REQUIRED,
65
				'owner of files which shall be moved'
66
			)
67
			->addArgument(
68
				'destination-user',
69
				InputArgument::REQUIRED,
70
				'user who will be the new owner of the files'
71
			)
72
			->addOption(
73
				'path',
74
				null,
75
				InputOption::VALUE_REQUIRED,
76
				'selectively provide the path to transfer. For example --path="folder_name"'
77
			);
78
	}
79
80
	protected function execute(InputInterface $input, OutputInterface $output) {
81
		$sourceUserObject = $this->userManager->get($input->getArgument('source-user'));
82
		$destinationUserObject = $this->userManager->get($input->getArgument('destination-user'));
83
		if ($sourceUserObject === null) {
84
			$output->writeln("<error>Unknown source user {$input->getArgument('source-user')}</error>");
85
			return 1;
86
		}
87
		if ($destinationUserObject === null) {
88
			$output->writeln("<error>Unknown destination user {$input->getArgument('destination-user')}</error>");
89
			return 1;
90
		}
91
92
		$inputPath = $input->getOption('path');
93
		$inputPath = \ltrim($inputPath, '/');
94
95
		try {
96
			$this->service->transfer($sourceUserObject, $destinationUserObject, $inputPath);
97
		} catch (\Exception $e) {
98
			$output->writeln('<error>And error occurred during the transfer: '.$e->getMessage().'</error>');
99
			return 1;
100
		}
101
		$output->writeln('<info>Done!</info>');
102
		return 0;
103
}
104
105
106
}
107