Completed
Push — master ( aa1ba7...d2851f )
by Maxence
38:28
created

Index   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 19.28 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 7
dl 16
loc 83
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A run() 0 22 2
B liveCycle() 16 24 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * FullTextSearch - Full text search framework for Nextcloud
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2018
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\FullTextSearch\Cron;
29
30
use Exception;
31
use OC\BackgroundJob\TimedJob;
32
use OCA\FullTextSearch\AppInfo\Application;
33
use OCA\FullTextSearch\Exceptions\InterruptException;
34
use OCA\FullTextSearch\Model\Runner;
35
use OCA\FullTextSearch\Service\IndexService;
36
use OCA\FullTextSearch\Service\MiscService;
37
use OCA\FullTextSearch\Service\PlatformService;
38
use OCA\FullTextSearch\Service\ProviderService;
39
use OCA\FullTextSearch\Service\RunningService;
40
use OCP\AppFramework\QueryException;
41
use OCP\IUserManager;
42
43
44
class Index extends TimedJob {
45
46
	/** @var IUserManager */
47
	private $userManager;
48
49
	/** @var IndexService */
50
	private $indexService;
51
52
	/** @var PlatformService */
53
	private $platformService;
54
55
	/** @var ProviderService */
56
	private $providerService;
57
58
	/** @var MiscService */
59
	private $miscService;
60
61
	/** @var Runner */
62
	private $runner;
63
64
	public function __construct() {
65
		$this->setInterval(20 * 60); // 20 minutes
66
	}
67
68
69
	/**
70
	 * @param $argument
71
	 *
72
	 * @throws QueryException
73
	 */
74
	protected function run($argument) {
75
		$app = new Application();
76
		$c = $app->getContainer();
77
78
		$this->userManager = $c->query(IUserManager::class);
79
		$runningService = $c->query(RunningService::class);
80
		$this->runner = new Runner($runningService, 'cronIndex');
81
82
		$this->indexService = $c->query(IndexService::class);
83
		$this->platformService = $c->query(PlatformService::class);
84
		$this->providerService = $c->query(ProviderService::class);
85
		$this->miscService = $c->query(MiscService::class);
86
87
		try {
88
			$this->runner->start();
89
			$this->liveCycle();
90
			$this->runner->stop();
91
		} catch (Exception $e) {
92
			$this->runner->exception($e->getMessage(), true);
93
		}
94
95
	}
96
97
98
	/**
99
	 * @throws Exception
100
	 * @throws InterruptException
101
	 */
102
	private function liveCycle() {
103
104
		$platform = $this->platformService->getPlatform(true);
105
		$indexes = $this->indexService->getQueuedIndexes();
106
107 View Code Duplication
		foreach ($indexes as $index) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
108
			$this->runner->update('indexing');
109
110
			try {
111
				$provider = $this->providerService->getProvider($index->getProviderId());
112
				if (!$this->providerService->isProviderIndexed($provider->getId())) {
113
					continue;
114
				}
115
116
				$this->indexService->updateDocument($platform, $provider, $index);
117
			} catch (Exception $e) {
118
				$this->runner->exception($e->getMessage(), false);
119
				// TODO - upgrade error number - after too many errors, delete index
120
				// TODO - do not count error if elasticsearch is down.
121
			}
122
		}
123
124
125
	}
126
}
127