Passed
Push — master ( 701550...dfe85a )
by Roeland
16:47 queued 13s
created

TransferOwnership::walkFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright Copyright (c) 2016, ownCloud, Inc.
5
 *
6
 * @author Carla Schroder <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Sujith H <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OCA\Files\Command;
31
32
use OCA\Files\Exception\TransferOwnershipException;
33
use OCA\Files\Service\OwnershipTransferService;
34
use OCP\IUser;
35
use OCP\IUserManager;
36
use Symfony\Component\Console\Command\Command;
37
use Symfony\Component\Console\Input\InputArgument;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Input\InputOption;
40
use Symfony\Component\Console\Output\OutputInterface;
41
42
class TransferOwnership extends Command {
43
44
	/** @var IUserManager */
45
	private $userManager;
46
47
	/** @var OwnershipTransferService */
48
	private $transferService;
49
50
	public function __construct(IUserManager $userManager,
51
								OwnershipTransferService $transferService) {
52
		parent::__construct();
53
		$this->userManager = $userManager;
54
		$this->transferService = $transferService;
55
	}
56
57
	protected function configure() {
58
		$this
59
			->setName('files:transfer-ownership')
60
			->setDescription('All files and folders are moved to another user - shares are moved as well.')
61
			->addArgument(
62
				'source-user',
63
				InputArgument::REQUIRED,
64
				'owner of files which shall be moved'
65
			)
66
			->addArgument(
67
				'destination-user',
68
				InputArgument::REQUIRED,
69
				'user who will be the new owner of the files'
70
			)
71
			->addOption(
72
				'path',
73
				null,
74
				InputOption::VALUE_REQUIRED,
75
				'selectively provide the path to transfer. For example --path="folder_name"',
76
				''
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
84
		if (!$sourceUserObject instanceof IUser) {
85
			$output->writeln("<error>Unknown source user " . $input->getArgument('source-user') . "</error>");
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('source-user') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
			$output->writeln("<error>Unknown source user " . /** @scrutinizer ignore-type */ $input->getArgument('source-user') . "</error>");
Loading history...
86
			return 1;
87
		}
88
89
		if (!$destinationUserObject instanceof IUser) {
90
			$output->writeln("<error>Unknown destination user " . $input->getArgument('destination-user') . "</error>");
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('destination-user') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
			$output->writeln("<error>Unknown destination user " . /** @scrutinizer ignore-type */ $input->getArgument('destination-user') . "</error>");
Loading history...
91
			return 1;
92
		}
93
94
		try {
95
			$this->transferService->transfer(
96
				$sourceUserObject,
97
				$destinationUserObject,
98
				ltrim($input->getOption('path'), '/'),
99
				$output
100
			);
101
		} catch (TransferOwnershipException $e) {
102
			$output->writeln("<error>" . $e->getMessage() . "</error>");
103
			return $e->getCode() !== 0 ? $e->getCode() : 1;
104
		}
105
106
		return 0;
107
	}
108
109
}
110