Completed
Push — master ( 2565e2...2c8d71 )
by Julius
05:36 queued 11s
created

DiscoveryManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 7
loc 63
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A get() 0 15 3
A fetchFromRemote() 0 17 3
A refetch() 0 4 1

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
 * @copyright Copyright (c) 2016 Lukas Reschke <[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
namespace OCA\Richdocuments\WOPI;
23
24
use OCP\Http\Client\IClientService;
25
use OCP\ICache;
26
use OCP\ICacheFactory;
27
use OCP\IConfig;
28
29
class DiscoveryManager {
30
31
	/** @var IClientService */
32
	private $clientService;
33
	/** @var ICache */
34
	private $cache;
35
	/** @var IConfig */
36
	private $config;
37
38
	/** @var string */
39
	private $discovery;
40
41 View Code Duplication
	public function __construct(IClientService $clientService,
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...
42
								ICacheFactory $cacheFactory,
43
								IConfig $config) {
44
		$this->clientService = $clientService;
45
		$this->cache = $cacheFactory->createDistributed('richdocuments');
46
		$this->config = $config;
47
	}
48
49
	public function get() {
50
		if ($this->discovery) {
51
			return $this->discovery;
52
		}
53
54
		$this->discovery = $this->cache->get('discovery');
55
		if (!$this->discovery) {
56
			$response = $this->fetchFromRemote();
57
			$responseBody = $response->getBody();
58
			$this->discovery = $responseBody;
0 ignored issues
show
Documentation Bug introduced by
It seems like $responseBody can also be of type resource. However, the property $discovery is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
59
			$this->cache->set('discovery', $this->discovery, 3600);
60
		}
61
62
		return $this->discovery;
63
	}
64
65
	/**
66
	 * @return \OCP\Http\Client\IResponse
67
	 * @throws \Exception
68
	 */
69
	public function fetchFromRemote() {
70
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
71
		$wopiDiscovery = $remoteHost . '/hosting/discovery';
72
73
		$client = $this->clientService->newClient();
74
		$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]];
75
76
		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
77
			$options['verify'] = false;
78
		}
79
80
		try {
81
			return $client->get($wopiDiscovery, $options);
82
		} catch (\Exception $e) {
83
			throw $e;
84
		}
85
	}
86
87
	public function refetch() {
88
		$this->cache->remove('discovery');
89
		$this->discovery = null;
90
	}
91
}
92