Completed
Pull Request — master (#544)
by Maxence
02:26
created

WebfingerHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 30 5
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
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 2017
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\Circles\Handlers;
28
29
30
use daita\MySmallPhpTools\Exceptions\SignatoryException;
31
use daita\MySmallPhpTools\Traits\TArrayTools;
32
use OC\URLGenerator;
33
use OCA\Circles\AppInfo\Application;
34
use OCA\Circles\Service\ConfigService;
35
use OCA\Circles\Service\RemoteService;
36
use OCP\Http\WellKnown\IHandler;
37
use OCP\Http\WellKnown\IRequestContext;
38
use OCP\Http\WellKnown\IResponse;
39
use OCP\Http\WellKnown\JrdResponse;
40
use OCP\IURLGenerator;
41
42
43
/**
44
 * Class WebfingerHandler
45
 *
46
 * @package OCA\Circles\Handlers
47
 */
48
class WebfingerHandler implements IHandler {
49
50
51
	use TArrayTools;
52
53
54
	/** @var URLGenerator */
55
	private $urlGenerator;
56
57
	/** @var RemoteService */
58
	private $remoteService;
59
60
	/** @var ConfigService */
61
	private $configService;
62
63
64
	/**
65
	 * WebfingerHandler constructor.
66
	 *
67
	 * @param IURLGenerator $urlGenerator
68
	 * @param RemoteService $remoteService
69
	 * @param ConfigService $configService
70
	 */
71
	public function __construct(
72
		IURLGenerator $urlGenerator, RemoteService $remoteService, ConfigService $configService
73
	) {
74
		$this->urlGenerator = $urlGenerator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $urlGenerator of type object<OCP\IURLGenerator> is incompatible with the declared type object<OC\URLGenerator> of property $urlGenerator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
		$this->remoteService = $remoteService;
76
		$this->configService = $configService;
77
	}
78
79
80
	/**
81
	 * @param string $service
82
	 * @param IRequestContext $context
83
	 * @param IResponse|null $response
84
	 *
85
	 * @return IResponse|null
86
	 */
87
	public function handle(string $service, IRequestContext $context, ?IResponse $response): ?IResponse {
88
		if ($service !== 'webfinger') {
89
			return $response;
90
		}
91
92
		$subject = $this->get('resource', $context->getHttpRequest()->getParams());
93
		if ($subject !== Application::APP_SUBJECT) {
94
			return $response;
95
		}
96
97
		if (!($response instanceof JrdResponse)) {
0 ignored issues
show
Bug introduced by
The class OCP\Http\WellKnown\JrdResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
98
			$response = new JrdResponse($subject);
99
		}
100
101
		try {
102
			$this->remoteService->getAppSignatory();
103
		} catch (SignatoryException $e) {
104
			return $response;
105
		}
106
107
		$href = $this->configService->getRemotePath();
108
		$info = [
109
			'app'     => Application::APP_ID,
110
			'name'    => Application::APP_NAME,
111
			'version' => $this->configService->getAppValue('installed_version'),
112
			'api'     => Application::APP_API
113
		];
114
115
		return $response->addLink(Application::APP_REL, 'application/json', $href, [], $info);
116
	}
117
118
}
119
120