Passed
Push — master ( 76d448...3ef509 )
by Robin
17:04 queued 13s
created

Delete::execute()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 16
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 25
rs 9.4222
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2023 Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files\Command\Object;
25
26
use OCP\DB\QueryBuilder\IQueryBuilder;
27
use OCP\IDBConnection;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Helper\QuestionHelper;
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
use Symfony\Component\Console\Question\ConfirmationQuestion;
35
36
class Delete extends Command {
37
	private ObjectUtil $objectUtils;
38
39
	public function __construct(ObjectUtil $objectUtils) {
40
		$this->objectUtils = $objectUtils;
41
		parent::__construct();
42
	}
43
44
	protected function configure(): void {
45
		$this
46
			->setName('files:object:delete')
47
			->setDescription('Delete an object from the object store')
48
			->addArgument('object', InputArgument::REQUIRED, "Object to delete")
49
			->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, "Bucket to delete the object from, only required in cases where it can't be determined from the config");
50
	}
51
52
	public function execute(InputInterface $input, OutputInterface $output): int {
53
		$object = $input->getArgument('object');
54
		$objectStore = $this->objectUtils->getObjectStore($input->getOption("bucket"), $output);
55
		if (!$objectStore) {
56
			return -1;
57
		}
58
59
		if ($fileId = $this->objectUtils->objectExistsInDb($object)) {
60
			$output->writeln("<error>Warning, object $object belongs to an existing file, deleting the object will lead to unexpected behavior if not replaced</error>");
61
			$output->writeln("  Note: use <info>occ files:delete $fileId</info> to delete the file cleanly or <info>occ info:file $fileId</info> for more information about the file");
62
			$output->writeln("");
63
		}
64
65
		if (!$objectStore->objectExists($object)) {
66
			$output->writeln("<error>Object $object does not exist</error>");
67
			return -1;
68
		}
69
70
		/** @var QuestionHelper $helper */
71
		$helper = $this->getHelper('question');
72
		$question = new ConfirmationQuestion("Delete $object? [y/N] ", false);
73
		if ($helper->ask($input, $output, $question)) {
74
			$objectStore->deleteObject($object);
75
		}
76
		return 0;
77
	}
78
}
79