Passed
Push — master ( 949f4a...10ae7a )
by Roeland
10:30 queued 13s
created
lib/private/Federation/CloudFederationProviderManager.php 2 patches
Indentation   +197 added lines, -197 removed lines patch added patch discarded remove patch
@@ -42,203 +42,203 @@
 block discarded – undo
42 42
  */
43 43
 class CloudFederationProviderManager implements ICloudFederationProviderManager {
44 44
 
45
-	/** @var array list of available cloud federation providers */
46
-	private $cloudFederationProvider;
47
-
48
-	/** @var IAppManager */
49
-	private $appManager;
50
-
51
-	/** @var IClientService */
52
-	private $httpClientService;
53
-
54
-	/** @var ICloudIdManager */
55
-	private $cloudIdManager;
56
-
57
-	/** @var ILogger */
58
-	private $logger;
59
-
60
-	/** @var array cache OCM end-points */
61
-	private $ocmEndPoints = [];
62
-
63
-	private $supportedAPIVersion = '1.0-proposal1';
64
-
65
-	/**
66
-	 * CloudFederationProviderManager constructor.
67
-	 *
68
-	 * @param IAppManager $appManager
69
-	 * @param IClientService $httpClientService
70
-	 * @param ICloudIdManager $cloudIdManager
71
-	 * @param ILogger $logger
72
-	 */
73
-	public function __construct(IAppManager $appManager,
74
-								IClientService $httpClientService,
75
-								ICloudIdManager $cloudIdManager,
76
-								ILogger $logger) {
77
-		$this->cloudFederationProvider= [];
78
-		$this->appManager = $appManager;
79
-		$this->httpClientService = $httpClientService;
80
-		$this->cloudIdManager = $cloudIdManager;
81
-		$this->logger = $logger;
82
-	}
83
-
84
-
85
-	/**
86
-	 * Registers an callback function which must return an cloud federation provider
87
-	 *
88
-	 * @param string $resourceType which resource type does the provider handles
89
-	 * @param string $displayName user facing name of the federated share provider
90
-	 * @param callable $callback
91
-	 */
92
-	public function addCloudFederationProvider($resourceType, $displayName, callable $callback) {
93
-		$this->cloudFederationProvider[$resourceType] = [
94
-			'resourceType' => $resourceType,
95
-			'displayName' => $displayName,
96
-			'callback' => $callback,
97
-		];
98
-
99
-	}
100
-
101
-	/**
102
-	 * remove cloud federation provider
103
-	 *
104
-	 * @param string $providerId
105
-	 */
106
-	public function removeCloudFederationProvider($providerId) {
107
-		unset($this->cloudFederationProvider[$providerId]);
108
-	}
109
-
110
-	/**
111
-	 * get a list of all cloudFederationProviders
112
-	 *
113
-	 * @return array [resourceType => ['resourceType' => $resourceType, 'displayName' => $displayName, 'callback' => callback]]
114
-	 */
115
-	public function getAllCloudFederationProviders() {
116
-		return $this->cloudFederationProvider;
117
-	}
118
-
119
-	/**
120
-	 * get a specific cloud federation provider
121
-	 *
122
-	 * @param string $resourceType
123
-	 * @return ICloudFederationProvider
124
-	 * @throws ProviderDoesNotExistsException
125
-	 */
126
-	public function getCloudFederationProvider($resourceType) {
127
-		if (isset($this->cloudFederationProvider[$resourceType])) {
128
-			return call_user_func($this->cloudFederationProvider[$resourceType]['callback']);
129
-		} else {
130
-			throw new ProviderDoesNotExistsException($resourceType);
131
-		}
132
-	}
133
-
134
-	public function sendShare(ICloudFederationShare $share) {
135
-		$cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith());
136
-		$ocmEndPoint = $this->getOCMEndPoint($cloudID->getRemote());
137
-		if (empty($ocmEndPoint)) {
138
-			return false;
139
-		}
140
-
141
-		$client = $this->httpClientService->newClient();
142
-		try {
143
-			$response = $client->post($ocmEndPoint . '/shares', [
144
-				'body' => json_encode($share->getShare()),
145
-				'headers' => ['content-type' => 'application/json'],
146
-				'timeout' => 10,
147
-				'connect_timeout' => 10,
148
-			]);
149
-
150
-			if ($response->getStatusCode() === Http::STATUS_CREATED) {
151
-				$result = json_decode($response->getBody(), true);
152
-				return (is_array($result)) ? $result : [];
153
-			}
154
-
155
-		} catch (\Exception $e) {
156
-			// if flat re-sharing is not supported by the remote server
157
-			// we re-throw the exception and fall back to the old behaviour.
158
-			// (flat re-shares has been introduced in Nextcloud 9.1)
159
-			if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
160
-				$this->logger->debug($e->getMessage());
161
-				throw $e;
162
-			}
163
-		}
164
-
165
-		return false;
166
-
167
-	}
168
-
169
-	/**
170
-	 * @param string $url
171
-	 * @param ICloudFederationNotification $notification
172
-	 * @return mixed
173
-	 */
174
-	public function sendNotification($url, ICloudFederationNotification $notification) {
175
-		$ocmEndPoint = $this->getOCMEndPoint($url);
176
-
177
-		if (empty($ocmEndPoint)) {
178
-			return false;
179
-		}
180
-
181
-		$client = $this->httpClientService->newClient();
182
-		try {
183
-			$response = $client->post($ocmEndPoint . '/notifications', [
184
-				'body' => json_encode($notification->getMessage()),
185
-				'headers' => ['content-type' => 'application/json'],
186
-				'timeout' => 10,
187
-				'connect_timeout' => 10,
188
-			]);
189
-			if ($response->getStatusCode() === Http::STATUS_CREATED) {
190
-				$result = json_decode($response->getBody(), true);
191
-				return (is_array($result)) ? $result : [];
192
-			}
193
-		} catch (\Exception $e) {
194
-			// log the error and return false
195
-			$this->logger->error('error while sending notification for federated share: ' . $e->getMessage());
196
-		}
197
-
198
-		return false;
199
-	}
200
-
201
-	/**
202
-	 * check if the new cloud federation API is ready to be used
203
-	 *
204
-	 * @return bool
205
-	 */
206
-	public function isReady() {
207
-		return $this->appManager->isEnabledForUser('cloud_federation_api');
208
-	}
209
-	/**
210
-	 * check if server supports the new OCM api and ask for the correct end-point
211
-	 *
212
-	 * @param string $url full base URL of the cloud server
213
-	 * @return string
214
-	 */
215
-	protected function getOCMEndPoint($url) {
216
-
217
-		if (isset($this->ocmEndPoints[$url])) {
218
-			return $this->ocmEndPoints[$url];
219
-		}
220
-
221
-		$client = $this->httpClientService->newClient();
222
-		try {
223
-			$response = $client->get($url . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]);
224
-		} catch (\Exception $e) {
225
-			$this->ocmEndPoints[$url] = '';
226
-			return '';
227
-		}
228
-
229
-		$result = $response->getBody();
230
-		$result = json_decode($result, true);
231
-
232
-		$supportedVersion = isset($result['apiVersion']) && $result['apiVersion'] === $this->supportedAPIVersion;
233
-
234
-		if (isset($result['endPoint']) && $supportedVersion) {
235
-			$this->ocmEndPoints[$url] = $result['endPoint'];
236
-			return $result['endPoint'];
237
-		}
238
-
239
-		$this->ocmEndPoints[$url] = '';
240
-		return '';
241
-	}
45
+    /** @var array list of available cloud federation providers */
46
+    private $cloudFederationProvider;
47
+
48
+    /** @var IAppManager */
49
+    private $appManager;
50
+
51
+    /** @var IClientService */
52
+    private $httpClientService;
53
+
54
+    /** @var ICloudIdManager */
55
+    private $cloudIdManager;
56
+
57
+    /** @var ILogger */
58
+    private $logger;
59
+
60
+    /** @var array cache OCM end-points */
61
+    private $ocmEndPoints = [];
62
+
63
+    private $supportedAPIVersion = '1.0-proposal1';
64
+
65
+    /**
66
+     * CloudFederationProviderManager constructor.
67
+     *
68
+     * @param IAppManager $appManager
69
+     * @param IClientService $httpClientService
70
+     * @param ICloudIdManager $cloudIdManager
71
+     * @param ILogger $logger
72
+     */
73
+    public function __construct(IAppManager $appManager,
74
+                                IClientService $httpClientService,
75
+                                ICloudIdManager $cloudIdManager,
76
+                                ILogger $logger) {
77
+        $this->cloudFederationProvider= [];
78
+        $this->appManager = $appManager;
79
+        $this->httpClientService = $httpClientService;
80
+        $this->cloudIdManager = $cloudIdManager;
81
+        $this->logger = $logger;
82
+    }
83
+
84
+
85
+    /**
86
+     * Registers an callback function which must return an cloud federation provider
87
+     *
88
+     * @param string $resourceType which resource type does the provider handles
89
+     * @param string $displayName user facing name of the federated share provider
90
+     * @param callable $callback
91
+     */
92
+    public function addCloudFederationProvider($resourceType, $displayName, callable $callback) {
93
+        $this->cloudFederationProvider[$resourceType] = [
94
+            'resourceType' => $resourceType,
95
+            'displayName' => $displayName,
96
+            'callback' => $callback,
97
+        ];
98
+
99
+    }
100
+
101
+    /**
102
+     * remove cloud federation provider
103
+     *
104
+     * @param string $providerId
105
+     */
106
+    public function removeCloudFederationProvider($providerId) {
107
+        unset($this->cloudFederationProvider[$providerId]);
108
+    }
109
+
110
+    /**
111
+     * get a list of all cloudFederationProviders
112
+     *
113
+     * @return array [resourceType => ['resourceType' => $resourceType, 'displayName' => $displayName, 'callback' => callback]]
114
+     */
115
+    public function getAllCloudFederationProviders() {
116
+        return $this->cloudFederationProvider;
117
+    }
118
+
119
+    /**
120
+     * get a specific cloud federation provider
121
+     *
122
+     * @param string $resourceType
123
+     * @return ICloudFederationProvider
124
+     * @throws ProviderDoesNotExistsException
125
+     */
126
+    public function getCloudFederationProvider($resourceType) {
127
+        if (isset($this->cloudFederationProvider[$resourceType])) {
128
+            return call_user_func($this->cloudFederationProvider[$resourceType]['callback']);
129
+        } else {
130
+            throw new ProviderDoesNotExistsException($resourceType);
131
+        }
132
+    }
133
+
134
+    public function sendShare(ICloudFederationShare $share) {
135
+        $cloudID = $this->cloudIdManager->resolveCloudId($share->getShareWith());
136
+        $ocmEndPoint = $this->getOCMEndPoint($cloudID->getRemote());
137
+        if (empty($ocmEndPoint)) {
138
+            return false;
139
+        }
140
+
141
+        $client = $this->httpClientService->newClient();
142
+        try {
143
+            $response = $client->post($ocmEndPoint . '/shares', [
144
+                'body' => json_encode($share->getShare()),
145
+                'headers' => ['content-type' => 'application/json'],
146
+                'timeout' => 10,
147
+                'connect_timeout' => 10,
148
+            ]);
149
+
150
+            if ($response->getStatusCode() === Http::STATUS_CREATED) {
151
+                $result = json_decode($response->getBody(), true);
152
+                return (is_array($result)) ? $result : [];
153
+            }
154
+
155
+        } catch (\Exception $e) {
156
+            // if flat re-sharing is not supported by the remote server
157
+            // we re-throw the exception and fall back to the old behaviour.
158
+            // (flat re-shares has been introduced in Nextcloud 9.1)
159
+            if ($e->getCode() === Http::STATUS_INTERNAL_SERVER_ERROR) {
160
+                $this->logger->debug($e->getMessage());
161
+                throw $e;
162
+            }
163
+        }
164
+
165
+        return false;
166
+
167
+    }
168
+
169
+    /**
170
+     * @param string $url
171
+     * @param ICloudFederationNotification $notification
172
+     * @return mixed
173
+     */
174
+    public function sendNotification($url, ICloudFederationNotification $notification) {
175
+        $ocmEndPoint = $this->getOCMEndPoint($url);
176
+
177
+        if (empty($ocmEndPoint)) {
178
+            return false;
179
+        }
180
+
181
+        $client = $this->httpClientService->newClient();
182
+        try {
183
+            $response = $client->post($ocmEndPoint . '/notifications', [
184
+                'body' => json_encode($notification->getMessage()),
185
+                'headers' => ['content-type' => 'application/json'],
186
+                'timeout' => 10,
187
+                'connect_timeout' => 10,
188
+            ]);
189
+            if ($response->getStatusCode() === Http::STATUS_CREATED) {
190
+                $result = json_decode($response->getBody(), true);
191
+                return (is_array($result)) ? $result : [];
192
+            }
193
+        } catch (\Exception $e) {
194
+            // log the error and return false
195
+            $this->logger->error('error while sending notification for federated share: ' . $e->getMessage());
196
+        }
197
+
198
+        return false;
199
+    }
200
+
201
+    /**
202
+     * check if the new cloud federation API is ready to be used
203
+     *
204
+     * @return bool
205
+     */
206
+    public function isReady() {
207
+        return $this->appManager->isEnabledForUser('cloud_federation_api');
208
+    }
209
+    /**
210
+     * check if server supports the new OCM api and ask for the correct end-point
211
+     *
212
+     * @param string $url full base URL of the cloud server
213
+     * @return string
214
+     */
215
+    protected function getOCMEndPoint($url) {
216
+
217
+        if (isset($this->ocmEndPoints[$url])) {
218
+            return $this->ocmEndPoints[$url];
219
+        }
220
+
221
+        $client = $this->httpClientService->newClient();
222
+        try {
223
+            $response = $client->get($url . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]);
224
+        } catch (\Exception $e) {
225
+            $this->ocmEndPoints[$url] = '';
226
+            return '';
227
+        }
228
+
229
+        $result = $response->getBody();
230
+        $result = json_decode($result, true);
231
+
232
+        $supportedVersion = isset($result['apiVersion']) && $result['apiVersion'] === $this->supportedAPIVersion;
233
+
234
+        if (isset($result['endPoint']) && $supportedVersion) {
235
+            $this->ocmEndPoints[$url] = $result['endPoint'];
236
+            return $result['endPoint'];
237
+        }
238
+
239
+        $this->ocmEndPoints[$url] = '';
240
+        return '';
241
+    }
242 242
 
243 243
 
244 244
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 								IClientService $httpClientService,
75 75
 								ICloudIdManager $cloudIdManager,
76 76
 								ILogger $logger) {
77
-		$this->cloudFederationProvider= [];
77
+		$this->cloudFederationProvider = [];
78 78
 		$this->appManager = $appManager;
79 79
 		$this->httpClientService = $httpClientService;
80 80
 		$this->cloudIdManager = $cloudIdManager;
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 
141 141
 		$client = $this->httpClientService->newClient();
142 142
 		try {
143
-			$response = $client->post($ocmEndPoint . '/shares', [
143
+			$response = $client->post($ocmEndPoint.'/shares', [
144 144
 				'body' => json_encode($share->getShare()),
145 145
 				'headers' => ['content-type' => 'application/json'],
146 146
 				'timeout' => 10,
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 
181 181
 		$client = $this->httpClientService->newClient();
182 182
 		try {
183
-			$response = $client->post($ocmEndPoint . '/notifications', [
183
+			$response = $client->post($ocmEndPoint.'/notifications', [
184 184
 				'body' => json_encode($notification->getMessage()),
185 185
 				'headers' => ['content-type' => 'application/json'],
186 186
 				'timeout' => 10,
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
 			}
193 193
 		} catch (\Exception $e) {
194 194
 			// log the error and return false
195
-			$this->logger->error('error while sending notification for federated share: ' . $e->getMessage());
195
+			$this->logger->error('error while sending notification for federated share: '.$e->getMessage());
196 196
 		}
197 197
 
198 198
 		return false;
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
 
221 221
 		$client = $this->httpClientService->newClient();
222 222
 		try {
223
-			$response = $client->get($url . '/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]);
223
+			$response = $client->get($url.'/ocm-provider/', ['timeout' => 10, 'connect_timeout' => 10]);
224 224
 		} catch (\Exception $e) {
225 225
 			$this->ocmEndPoints[$url] = '';
226 226
 			return '';
Please login to merge, or discard this patch.