Completed
Push — master ( f0b8b1...988dc2 )
by Maxence
02:48 queued 57s
created

CliService   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 186
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setRunner() 0 5 1
A createPanel() 0 7 2
A initDisplay() 0 3 1
A displayPanel() 0 6 1
A currentPanel() 0 9 3
A switchPanel() 0 13 2
A runDisplay() 0 24 4
A refreshDisplay() 0 18 4
A refreshInfo() 0 9 2
A onInfoUpdated() 0 13 3
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\FullTextSearch\Service;
28
29
use OCA\FullTextSearch\Model\Runner;
30
use Symfony\Component\Console\Helper\ProgressBar;
31
use Symfony\Component\Console\Output\OutputInterface;
32
33
class CliService {
34
35
36
	/** @var MiscService */
37
	private $miscService;
38
39
	/** @var Runner */
40
	private $runner;
41
42
	/** @var array */
43
	private $panels = [];
44
45
	/** @var array */
46
	private $displayedPanel = [];
47
48
	/** @var ProgressBar */
49
	private $display;
50
51
	/** @var OutputInterface */
52
	private $output = null;
53
54
	/**
55
	 * TestService constructor.
56
	 *
57
	 * @param MiscService $miscService
58
	 */
59
	public function __construct(MiscService $miscService) {
60
		$this->miscService = $miscService;
61
	}
62
63
64
	/**
65
	 * @param Runner $runner
66
	 */
67
	public function setRunner(Runner $runner) {
68
		$this->runner = $runner;
69
70
		$this->runner->onInfoUpdate([$this, 'onInfoUpdated']);
71
	}
72
73
74
	/**
75
	 * @param string $panelId
76
	 * @param array $lines
77
	 */
78
	public function createPanel($panelId, $lines) {
79
		if (!is_array($lines)) {
80
			$lines = [$lines];
81
		}
82
83
		$this->panels[$panelId] = $lines;
84
	}
85
86
87
	/**
88
	 *
89
	 */
90
	public function initDisplay() {
91
		$this->displayedPanel = [];
92
	}
93
94
95
	/**
96
	 * @param string $panelSlot
97
	 * @param string $panelId
98
	 */
99
	public function displayPanel($panelSlot, $panelId) {
100
		$this->displayedPanel[] = [
101
			'slot' => $panelSlot,
102
			'id'   => $panelId
103
		];
104
	}
105
106
107
	/**
108
	 * @param string $panelSlot
109
	 *
110
	 * @return string
111
	 */
112
	public function currentPanel($panelSlot) {
113
		foreach ($this->displayedPanel as $panel) {
114
			if ($panel['slot'] === $panelSlot) {
115
				return $panel['id'];
116
			}
117
		}
118
119
		return '';
120
	}
121
122
123
	/**
124
	 * @param string $panelSlot
125
	 * @param string $panelId
126
	 */
127
	public function switchPanel($panelSlot, $panelId) {
128
		$this->displayedPanel = array_map(
129
			function($item) use ($panelId, $panelSlot) {
130
				if ($item['slot'] === $panelSlot) {
131
					$item['id'] = $panelId;
132
				}
133
134
				return $item;
135
			}, $this->displayedPanel
136
		);
137
138
		$this->refreshDisplay();
139
	}
140
141
	/**
142
	 * @param OutputInterface $output
143
	 * @param array $initVar
144
	 */
145
	public function runDisplay(OutputInterface $output, $initVar = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $initVar is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
		$this->output = $output;
147
148
		$output->writeLn('');
149
		foreach ($this->displayedPanel as $displayedPanel) {
150
			$panel = $this->panels[$displayedPanel['id']];
151
			for ($i = 0; $i < sizeof($panel); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
152
				$output->writeLn('');
153
			}
154
		}
155
156
		$this->display = new ProgressBar($this->output);
157
		$this->display->setOverwrite(true);
158
159
		$initVar = $this->runner->getInfo();
160
		$keys = array_keys($initVar);
161
		foreach ($keys as $key) {
162
			$this->display->setMessage($initVar[$key], $key);
163
		}
164
165
		$this->display->clear();
166
167
		$this->refreshDisplay();
168
	}
169
170
171
	public function refreshDisplay() {
172
173
		if ($this->display === null) {
174
			return;
175
		}
176
177
		$format = [];
178
		foreach ($this->displayedPanel as $displayedPanel) {
179
			$panel = $this->panels[$displayedPanel['id']];
180
			for ($i = 0; $i < sizeof($panel); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
181
				$format[] = $panel[$i];
182
			}
183
		}
184
185
		$this->display->setFormat(implode("\n", $format) . "\n");
186
		$this->refreshInfo();
187
		$this->display->start();
188
	}
189
190
191
	public function refreshInfo() {
192
		if ($this->runner->isPauseRunning()) {
193
			$this->display->setMessage('(paused)', '_paused');
194
		} else {
195
			$this->display->setMessage('', '_paused');
196
		}
197
198
		$this->display->display();
199
	}
200
201
	/**
202
	 * @param array $info
203
	 */
204
	public function onInfoUpdated($info) {
205
		if ($this->display === null) {
206
			return;
207
		}
208
209
		$keys = array_keys($info);
210
		foreach ($keys as $k) {
211
			$this->display->setMessage($info[$k], $k);
212
		}
213
		$this->refreshInfo();
214
215
		$this->display->display();
216
	}
217
218
}
219