Completed
Push — master ( 3aeedd...be0cda )
by Maxence
03:35 queued 11s
created

CirclesMaintenance::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Command;
33
34
35
use OC\Core\Command\Base;
36
use OCA\Circles\Db\CoreRequestBuilder;
37
use OCA\Circles\Service\MaintenanceService;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Input\InputOption;
40
use Symfony\Component\Console\Output\OutputInterface;
41
use Symfony\Component\Console\Question\ConfirmationQuestion;
42
use Symfony\Component\Console\Question\Question;
43
44
45
/**
46
 * Class CirclesMaintenance
47
 *
48
 * @package OCA\Circles\Command
49
 */
50
class CirclesMaintenance extends Base {
51
52
53
	/** @var CoreRequestBuilder */
54
	private $coreQueryBuilder;
55
56
	/** @var MaintenanceService */
57
	private $maintenanceService;
58
59
60
	/**
61
	 * CirclesMaintenance constructor.
62
	 *
63
	 * @param CoreRequestBuilder $coreQueryBuilder
64
	 * @param MaintenanceService $maintenanceService
65
	 */
66
	public function __construct(
67
		CoreRequestBuilder $coreQueryBuilder,
68
		MaintenanceService $maintenanceService
69
	) {
70
		parent::__construct();
71
		$this->coreQueryBuilder = $coreQueryBuilder;
72
		$this->maintenanceService = $maintenanceService;
73
	}
74
75
76
	protected function configure() {
77
		parent::configure();
78
		$this->setName('circles:maintenance')
79
			 ->setDescription('Clean stuff, keeps the app running')
80
			 ->addOption(
81
				 'reset', '', InputOption::VALUE_NONE, 'reset Circles; remove all data related to the App'
82
			 )
83
			 ->addOption(
84
				 'uninstall', '', InputOption::VALUE_NONE,
85
				 'Uninstall the apps and everything related to the app from the database'
86
			 );
87
	}
88
89
90
	/**
91
	 * @param InputInterface $input
92
	 * @param OutputInterface $output
93
	 *
94
	 * @return int
95
	 */
96
	protected function execute(InputInterface $input, OutputInterface $output): int {
97
		$reset = $input->getOption('reset');
98
		$uninstall = $input->getOption('uninstall');
99
100
		if ($reset || $uninstall) {
101
			$output->writeln('');
102
			$output->writeln('');
103
			$output->writeln(
104
				'<error>WARNING! You are about to delete all data related to the Circles App!</error>'
105
			);
106
			$question = new ConfirmationQuestion(
107
				'<comment>Do you really want to want to reset Circle ?</comment> (y/N) ', false, '/^(y|Y)/i'
108
			);
109
110
			$helper = $this->getHelper('question');
111
			if (!$helper->ask($input, $output, $question)) {
112
				$output->writeln('aborted.');
113
114
				return 0;
115
			}
116
117
			$output->writeln('');
118
			$output->writeln('<error>WARNING! This operation is not reversible.</error>');
119
120
			$keyword = ($uninstall) ? 'uninstall' : 'reset';
121
122
			$question = new Question(
123
				'<comment>Please confirm this destructive operation by typing \'' . $keyword
124
				. '\'</comment>: ', ''
125
			);
126
127
			$helper = $this->getHelper('question');
128
			$confirmation = $helper->ask($input, $output, $question);
129
			if (strtolower($confirmation) !== strtolower($keyword)) {
130
				$output->writeln('aborted.');
131
132
				return 0;
133
			}
134
135
			$this->coreQueryBuilder->cleanDatabase();
136
			if ($uninstall) {
137
				$this->coreQueryBuilder->uninstall();
138
			}
139
140
			$output->writeln('<info>' . $keyword . ' done</info>');
141
142
			return 0;
143
		}
144
145
		$this->maintenanceService->setOccOutput($output);
146
		$this->maintenanceService->runMaintenance();
147
148
		$output->writeln('');
149
		$output->writeln('<info>done</info>');
150
151
		return 0;
152
	}
153
154
}
155
156
157
158