Completed
Push — master ( c9612b...1c88e5 )
by Lukas
15:34
created

ShareInfoMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace OCA\Files_Sharing\Middleware;
4
5
use OCA\FederatedFileSharing\FederatedShareProvider;
6
use OCA\Files_Sharing\Controller\ShareInfoController;
7
use OCA\Files_Sharing\Exceptions\S2SException;
8
use OCP\AppFramework\Controller;
9
use OCP\AppFramework\Http;
10
use OCP\AppFramework\Http\JSONResponse;
11
use OCP\AppFramework\Http\Response;
12
use OCP\AppFramework\Middleware;
13
use OCP\Share\IManager;
14
15
class ShareInfoMiddleware extends Middleware {
16
	/** @var IManager */
17
	private $shareManager;
18
19
	public function __construct(IManager $shareManager) {
20
		$this->shareManager = $shareManager;
21
	}
22
23
	/**
24
	 * @param Controller $controller
25
	 * @param string $methodName
26
	 * @throws S2SException
27
	 */
28
	public function beforeController($controller, $methodName) {
29
		if (!($controller instanceof ShareInfoController)) {
30
			return;
31
		}
32
33
		if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
34
			throw new S2SException();
35
		}
36
	}
37
38
	/**
39
	 * @param Controller $controller
40
	 * @param string $methodName
41
	 * @param \Exception $exception
42
	 * @throws \Exception
43
	 * @return Response
44
	 */
45
	public function afterException($controller, $methodName, \Exception $exception) {
46
		if (!($controller instanceof ShareInfoController)) {
47
			throw $exception;
48
		}
49
50
		if ($exception instanceof S2SException) {
51
			return new JSONResponse([], Http::STATUS_NOT_FOUND);
52
		}
53
54
		throw $exception;
55
	}
56
57
	/**
58
	 * @param Controller $controller
59
	 * @param string $methodName
60
	 * @param Response $response
61
	 * @return Response
62
	 */
63
	public function afterController($controller, $methodName, Response $response) {
64
		if (!($controller instanceof ShareInfoController)) {
65
			return $response;
66
		}
67
68
		if (!($response instanceof JSONResponse)) {
69
			return $response;
70
		}
71
72
		$data = $response->getData();
73
		$status = 'error';
74
75
		if ($response->getStatus() === Http::STATUS_OK) {
76
			$status = 'success';
77
		}
78
79
		$response->setData([
80
			'data' => $data,
81
			'status' => $status,
82
		]);
83
84
		return $response;
85
	}
86
}
87