ToolsController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dataFromUrl() 0 14 2
A binFromExternalImage() 0 16 2
A fail() 0 6 1
A success() 0 6 1
1
<?php
2
/**
3
 * Mood
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
28
namespace OCA\Mood\Controller;
29
30
use OC\AppFramework\Http;
31
use OCA\Mood\Service\HttpService;
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http\DataDisplayResponse;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\AppFramework\Http\FileDisplayResponse;
36
use OCP\IRequest;
37
38
class ToolsController extends Controller {
39
40
	/** @var HttpService */
41
	private $httpService;
42
43
	public function __construct($appName, IRequest $request, HttpService $httpService) {
44
		parent::__construct($appName, $request);
45
46
		$this->httpService = $httpService;
47
	}
48
49
50
	/**
51
	 * @NoAdminRequired
52
	 *
53
	 * @param $url
54
	 *
55
	 * @return DataResponse
56
	 */
57
	public function dataFromUrl($url) {
58
59
		try {
60
			$data = $this->httpService->getMetaFromWebsite($url);
61
62
			return self::success(['url' => $url, 'data' => $data]);
63
		} catch (\Exception $e) {
64
			$error = $e->getMessage();
65
		}
66
67
		return self::fail(
68
			['url' => $url, 'error' => $error]
69
		);
70
	}
71
72
73
	/**
74
	 * @NoAdminRequired
75
	 * @NoCSRFRequired
76
	 *
77
	 * @param $url
78
	 *
79
	 * @return DataDisplayResponse|DataResponse|FileDisplayResponse
80
	 */
81
	public function binFromExternalImage($url) {
82
		try {
83
84
			$image = HttpService::file_get_contents_curl($url, true);
85
			$response =
86
				new DataDisplayResponse(
87
					$image, Http::STATUS_OK, ['Content-Type' => 'image/jpeg']
88
				);
89
90
			return $response;
91
		} catch (\Exception $e) {
92
			return new DataResponse([], Http::STATUS_NOT_FOUND);
93
		}
94
95
96
	}
97
98
99
	/**
100
	 * @param $data
101
	 *
102
	 * @return DataResponse
103
	 */
104
	public static function fail($data) {
105
		return new DataResponse(
106
			array_merge($data, array('status' => 0)),
107
			Http::STATUS_NON_AUTHORATIVE_INFORMATION
108
		);
109
	}
110
111
	/**
112
	 * @param $data
113
	 *
114
	 * @return DataResponse
115
	 */
116
	public static function success($data) {
117
		return new DataResponse(
118
			array_merge($data, array('status' => 1)),
119
			Http::STATUS_CREATED
120
		);
121
	}
122
}