Issues (2553)

core/Command/SystemTag/Edit.php (1 issue)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <[email protected]>
4
 *
5
 * @author Johannes Leuker <[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
namespace OC\Core\Command\SystemTag;
24
25
use OC\Core\Command\Base;
26
use OCP\SystemTag\ISystemTagManager;
27
use OCP\SystemTag\TagAlreadyExistsException;
28
use OCP\SystemTag\TagNotFoundException;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Input\InputOption;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class Edit extends Base {
34
	public function __construct(
35
		protected ISystemTagManager $systemTagManager,
36
	) {
37
		parent::__construct();
38
	}
39
40
	protected function configure() {
41
		$this
42
			->setName('tag:edit')
43
			->setDescription('edit tag attributes')
44
			->addArgument(
45
				'id',
46
				InputOption::VALUE_REQUIRED,
47
				'The ID of the tag that should be deleted',
48
			)
49
			->addOption(
50
				'name',
51
				null,
52
				InputOption::VALUE_OPTIONAL,
53
				'sets the \'name\' parameter',
54
			)
55
			->addOption(
56
				'access',
57
				null,
58
				InputOption::VALUE_OPTIONAL,
59
				'sets the access control level (public, restricted, invisible)',
60
			);
61
	}
62
63
	protected function execute(InputInterface $input, OutputInterface $output): int {
64
		$tagArray = $this->systemTagManager->getTagsByIds($input->getArgument('id'));
65
		// returns an array, but we always expect 0 or 1 results
66
67
		if (!$tagArray) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tagArray of type OCP\SystemTag\ISystemTag[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
			$output->writeln('<error>Tag not found</error>');
69
			return 3;
70
		}
71
72
		$tag = array_values($tagArray)[0];
73
		$name = $tag->getName();
74
		if (!empty($input->getOption('name'))) {
75
			$name = $input->getOption('name');
76
		}
77
78
		$userVisible = $tag->isUserVisible();
79
		$userAssignable = $tag->isUserAssignable();
80
		if ($input->getOption('access')) {
81
			switch ($input->getOption('access')) {
82
				case 'public':
83
					$userVisible = true;
84
					$userAssignable = true;
85
					break;
86
				case 'restricted':
87
					$userVisible = true;
88
					$userAssignable = false;
89
					break;
90
				case 'invisible':
91
					$userVisible = false;
92
					$userAssignable = false;
93
					break;
94
				default:
95
					$output->writeln('<error>`access` property is invalid</error>');
96
					return 1;
97
			}
98
		}
99
100
		try {
101
			$this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable);
102
			$output->writeln('<info>Tag updated ("' . $name . '", '. $userVisible . ', ' . $userAssignable . ')</info>');
103
			return 0;
104
		} catch (TagNotFoundException $e) {
105
			$output->writeln('<error>Tag not found</error>');
106
			return 1;
107
		} catch (TagAlreadyExistsException $e) {
108
			$output->writeln('<error>'.$e->getMessage().'</error>');
109
			return 2;
110
		}
111
	}
112
}
113