Completed
Push — master ( 81e73e...c48e81 )
by Maxence
01:56
created

Check::displayAsJson()   B

Complexity

Conditions 5
Paths 40

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
cc 5
eloc 28
nc 40
nop 0
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\Command;
28
29
use Exception;
30
use OCA\FullTextSearch\Model\ExtendedBase;
31
use OCA\FullTextSearch\Service\ConfigService;
32
use OCA\FullTextSearch\Service\MiscService;
33
use OCA\FullTextSearch\Service\PlatformService;
34
use OCA\FullTextSearch\Service\ProviderService;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Input\InputOption;
37
use Symfony\Component\Console\Output\OutputInterface;
38
39
40
class Check extends ExtendedBase {
41
42
	const CYCLE_DELAY = 10;
43
44
45
	/** @var ConfigService */
46
	private $configService;
47
48
	/** @var PlatformService */
49
	private $platformService;
50
51
	/** @var ProviderService */
52
	private $providerService;
53
54
	/** @var MiscService */
55
	private $miscService;
56
57
58
	/**
59
	 * Index constructor.
60
	 *
61
	 * @param ConfigService $configService
62
	 * @param PlatformService $platformService
63
	 * @param ProviderService $providerService
64
	 * @param MiscService $miscService
65
	 */
66 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
		ConfigService $configService, PlatformService $platformService,
68
		ProviderService $providerService, MiscService $miscService
69
	) {
70
		parent::__construct();
71
72
		$this->configService = $configService;
73
		$this->platformService = $platformService;
74
		$this->providerService = $providerService;
75
		$this->miscService = $miscService;
76
	}
77
78
79
	/**
80
	 *
81
	 */
82
	protected function configure() {
83
		parent::configure();
84
		$this->setName('fulltextsearch:check')
85
			 ->addOption('json', 'j', InputOption::VALUE_NONE, 'return result as JSON')
86
			 ->setDescription('Check the installation');
87
	}
88
89
90
	/**
91
	 * @param InputInterface $input
92
	 * @param OutputInterface $output
93
	 *
94
	 * @return int|null|void
95
	 * @throws Exception
96
	 */
97
	protected function execute(InputInterface $input, OutputInterface $output) {
98
99
		if ($input->getOption('json') === true) {
100
			$output->writeln(json_encode($this->displayAsJson(), JSON_PRETTY_PRINT));
101
102
			return;
103
		}
104
105
		$output->writeln(
106
			'Full text search ' . $this->configService->getAppValue('installed_version')
107
		);
108
		$output->writeln(' ');
109
110
		$this->displayPlatform($output);
111
		$this->displayProviders($output);
112
	}
113
114
115
	private function displayAsJson() {
116
117
		try {
118
			$platforms = $this->platformService->getPlatforms();
119
			$ak = array_keys($platforms);
120
			foreach ($ak as $k) {
121
				$platform = $platforms[$k];
122
				$platform->loadPlatform();
123
				$resultPlatform[$platform->getId()] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$resultPlatform was never initialized. Although not strictly required by PHP, it is generally a good practice to add $resultPlatform = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
124
					'class'   => $k,
125
					'version' => $platform->getVersion(),
126
					'config'  => $platform->getConfiguration()
127
				];
128
			}
129
130
		} catch (Exception $e) {
131
			$resultPlatform = ['error' => $e->getMessage()];
132
		}
133
134
		$resultProviders = [];
135
		try {
136
			$providers = $this->providerService->getProviders();
137
			foreach ($providers as $provider) {
138
				$resultProviders[$provider->getId()] = [
139
					'version' => $provider->getVersion(),
140
					'config'  => $provider->getConfiguration()
141
				];
142
			}
143
		} catch (Exception $e) {
144
			$resultProviders[] = ['error' => $e->getMessage()];
145
		}
146
147
		return [
148
			'fulltextsearch' => [
149
				'version' => $this->configService->getAppValue('installed_version'),
150
				'config'  => $this->configService->getConfig()
151
			],
152
153
			'platform'  => $resultPlatform,
0 ignored issues
show
Bug introduced by
The variable $resultPlatform does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
154
			'providers' => $resultProviders
155
		];
156
157
	}
158
159
160
	/**
161
	 * @param OutputInterface $output
162
	 *
163
	 * @throws Exception
164
	 */
165
	private function displayPlatform(OutputInterface $output) {
166
		try {
167
			$platform = $this->platformService->getPlatform();
168
		} catch (Exception $e) {
169
			$output->writeln('No search platform available');
170
171
			return;
172
		}
173
174
		$output->writeln('- Search Platform:');
175
176
		$output->writeln($platform->getName() . ' ' . $platform->getVersion());
177
		echo json_encode($platform->getConfiguration(), JSON_PRETTY_PRINT);
178
179
		$output->writeln(' ');
180
		$output->writeln(' ');
181
	}
182
183
184
	/**
185
	 * @param OutputInterface $output
186
	 *
187
	 * @throws Exception
188
	 */
189
	private function displayProviders(OutputInterface $output) {
190
		$providers = $this->providerService->getProviders();
191
192
		if (sizeof($providers) === 0) {
193
			$output->writeln('No Content Provider available');
194
195
			return;
196
		}
197
198
		$output->writeln('- Content Providers:');
199
200
		foreach ($providers as $provider) {
201
			$output->writeln($provider->getName() . ' ' . $provider->getVersion());
202
			echo json_encode($provider->getConfiguration(), JSON_PRETTY_PRINT);
203
			$output->writeln('');
204
		}
205
206
207
	}
208
209
210
}
211
212
213
214