Completed
Push — master ( ba9b17...94004c )
by Lukas
05:19 queued 04:59
created

RetryJob   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
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
30
class RetryJob extends Job {
31
	/** @var IClientService */
32
	private $clientService;
33
	/** @var IJobList */
34
	private $jobList;
35
	/** @var string */
36
	private $lookupServer = 'https://lookup.nextcloud.com/users';
37
38
	/**
39
	 * @param IClientService|null $clientService
40
	 * @param IJobList|null $jobList
41
	 */
42
	public function __construct(IClientService $clientService = null,
43
								IJobList $jobList = null) {
44
		if($clientService !== null) {
45
			$this->clientService = $clientService;
46
		} else {
47
			$this->clientService = \OC::$server->getHTTPClientService();
48
		}
49
		if($jobList !== null) {
50
			$this->jobList = $jobList;
51
		} else {
52
			$this->jobList = \OC::$server->getJobList();
53
		}
54
	}
55
56
	protected function run($argument) {
57
		if($argument['retryNo'] === 5) {
58
			return;
59
		}
60
61
		$client = $this->clientService->newClient();
62
63
		try {
64
			$client->post($this->lookupServer,
65
				[
66
					'body' => json_encode($argument['dataArray']),
67
					'timeout' => 10,
68
					'connect_timeout' => 3,
69
				]
70
			);
71
		} catch (\Exception $e) {
72
			$this->jobList->add(RetryJob::class,
73
				[
74
					'dataArray' => $argument['dataArray'],
75
					'retryNo' => $argument['retryNo'] + 1,
76
				]
77
			);
78
79
		}
80
	}
81
}
82