Completed
Pull Request — master (#178)
by Georg
04:24
created

ProxyController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 75
rs 10
c 1
b 0
f 0
ccs 34
cts 34
cp 1
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B proxy() 0 35 4
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use GuzzleHttp\Exception\RequestException;
25
use GuzzleHttp\Exception\ClientException;
26
use GuzzleHttp\Exception\ConnectException;
27
28
use OCA\Calendar\Http\StreamResponse;
29
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Controller;
33
use OCP\Http\Client\IClientService;
34
use OCP\IL10N;
35
use OCP\ILogger;
36
use OCP\IRequest;
37
38
class ProxyController extends Controller {
39
40
	/**
41
	 * @var IClientService
42
	 */
43
	protected $client;
44
45
	/**
46
	 * @var IL10N
47
	 */
48
	protected $l10n;
49
50
	/**
51
	 * @var ILogger
52
	 */
53
	protected $logger;
54
55
	/**
56
	 * @param string $appName
57
	 * @param IRequest $request an instance of the request
58
	 * @param IClientService $client
59
	 * @param IL10N $l10n
60
	 * @param ILogger $logger
61
	 */
62 4
	public function __construct($appName, IRequest $request,
63
								IClientService $client,
64
								IL10N $l10n, ILogger $logger) {
65 4
		parent::__construct($appName, $request);
66 4
		$this->client = $client;
67 4
		$this->l10n = $l10n;
68 4
		$this->logger = $logger;
69 4
	}
70
71
	/**
72
	 * @NoAdminRequired
73
	 *
74
	 * @param $url
75
	 * @return StreamResponse|JSONResponse
76
	 */
77 4
	public function proxy($url) {
78 4
		$client = $this->client->newClient();
79
		try {
80 4
			$clientResponse = $client->get($url, [
81 4
				'stream' => true,
82 4
			]);
83 1
			$response = new StreamResponse($clientResponse->getBody());
84 1
			$response->setHeaders([
85 1
				'Content-Type' => 'text/calendar',
86 1
			]);
87 4
		} catch (ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
88 1
			$error_code = $e->getResponse()->getStatusCode();
89 1
			$message = $e->getMessage();
90
91 1
			$this->logger->debug($message);
92 1
			$response = new JSONResponse([
93 1
				'message' => $message,
94
				'proxy_code' => $error_code
95 1
			], Http::STATUS_UNPROCESSABLE_ENTITY);
96 3
		} catch (ConnectException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ConnectException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
97 1
			$this->logger->debug($e->getMessage());
98 1
			$response = new JSONResponse([
99 1
				'message' => $this->l10n->t('Error connecting to remote server'),
100
				'proxy_code' => -1
101 1
			], Http::STATUS_UNPROCESSABLE_ENTITY);
102 2
		} catch (RequestException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\RequestException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
103 1
			$this->logger->debug($e->getMessage());
104 1
			$response = new JSONResponse([
105 1
				'message' => $this->l10n->t('Error requesting resource on remote server'),
106 1
				'proxy_code' => -2,
107 1
			], Http::STATUS_UNPROCESSABLE_ENTITY);
108
		}
109
110 4
		return $response;
111
	}
112
}
113