Completed
Pull Request — master (#3869)
by Morris
11:40
created

RetryJob   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6
lcom 2
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 8 2
B run() 0 25 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
23
namespace OCA\LookupServerConnector\BackgroundJobs;
24
25
26
use OC\BackgroundJob\Job;
27
use OCP\BackgroundJob\IJobList;
28
use OCP\Http\Client\IClientService;
29
use OCP\ILogger;
30
31
class RetryJob extends Job {
32
	/** @var IClientService */
33
	private $clientService;
34
	/** @var IJobList */
35
	private $jobList;
36
	/** @var string */
37
	private $lookupServer = 'https://lookup.nextcloud.com/users';
38
39
	/**
40
	 * @param IClientService $clientService
41
	 * @param IJobList $jobList
42
	 */
43
	public function __construct(IClientService $clientService,
44
								IJobList $jobList) {
45
		$this->clientService = $clientService;
46
		$this->jobList = $jobList;
47
	}
48
49
	/**
50
	 * run the job, then remove it from the jobList
51
	 *
52
	 * @param JobList $jobList
53
	 * @param ILogger $logger
54
	 */
55
	public function execute($jobList, ILogger $logger = null) {
56
57
		if ($this->shouldRun($this->argument)) {
0 ignored issues
show
Bug introduced by
The method shouldRun() does not seem to exist on object<OCA\LookupServerC...ackgroundJobs\RetryJob>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
			parent::execute($jobList, $logger);
59
			$jobList->remove($this, $this->argument);
60
		}
61
62
	}
63
64
	protected function run($argument) {
65
		if($argument['retryNo'] === 5) {
66
			return;
67
		}
68
69
		$client = $this->clientService->newClient();
70
71
		try {
72
			$client->post($this->lookupServer,
73
				[
74
					'body' => json_encode($argument['dataArray']),
75
					'timeout' => 10,
76
					'connect_timeout' => 3,
77
				]
78
			);
79
		} catch (\Exception $e) {
80
			$this->jobList->add(RetryJob::class,
81
				[
82
					'dataArray' => $argument['dataArray'],
83
					'retryNo' => $argument['retryNo'] + 1,
84
				]
85
			);
86
87
		}
88
	}
89
}
90