2222
2323namespace OC \Core \Controller ;
2424
25+ use Exception ;
2526use OCP \AppFramework \Http ;
2627use OCP \AppFramework \OCSController ;
2728use OCP \AppFramework \Http \DataResponse ;
3031use OCP \Collaboration \Resources \IManager ;
3132use OCP \Collaboration \Resources \IResource ;
3233use OCP \Collaboration \Resources \ResourceException ;
34+ use OCP \ILogger ;
3335use OCP \IRequest ;
3436use OCP \IUserSession ;
3537
3638class CollaborationResourcesController extends OCSController {
3739
3840 /** @var IManager */
3941 private $ manager ;
40-
4142 /** @var IUserSession */
4243 private $ userSession ;
44+ /** @var ILogger */
45+ private $ logger ;
4346
4447 public function __construct (
4548 string $ appName ,
4649 IRequest $ request ,
4750 IManager $ manager ,
48- IUserSession $ userSession
51+ IUserSession $ userSession ,
52+ ILogger $ logger
4953 ) {
5054 parent ::__construct ($ appName , $ request );
5155
5256 $ this ->manager = $ manager ;
5357 $ this ->userSession = $ userSession ;
58+ $ this ->logger = $ logger ;
5459 }
5560
5661 /**
@@ -81,7 +86,7 @@ public function listCollection(int $collectionId): DataResponse {
8186 return new DataResponse ([], Http::STATUS_NOT_FOUND );
8287 }
8388
84- return new DataResponse ( $ this ->prepareCollection ($ collection) );
89+ return $ this ->respondCollection ($ collection );
8590 }
8691
8792 /**
@@ -97,7 +102,7 @@ public function searchCollections(string $filter): DataResponse {
97102 return new DataResponse ([], Http::STATUS_NOT_FOUND );
98103 }
99104
100- return new DataResponse (array_map ([ $ this , ' prepareCollection ' ], $ collections ));
105+ return new DataResponse ($ this -> prepareCollections ( $ collections ));
101106 }
102107
103108 /**
@@ -126,7 +131,7 @@ public function addResource(int $collectionId, string $resourceType, string $res
126131 } catch (ResourceException $ e ) {
127132 }
128133
129- return new DataResponse ( $ this ->prepareCollection ($ collection) );
134+ return $ this ->respondCollection ($ collection );
130135 }
131136
132137 /**
@@ -152,7 +157,7 @@ public function removeResource(int $collectionId, string $resourceType, string $
152157
153158 $ collection ->removeResource ($ resource );
154159
155- return new DataResponse ( $ this ->prepareCollection ($ collection) );
160+ return $ this ->respondCollection ($ collection );
156161 }
157162
158163 /**
@@ -173,7 +178,7 @@ public function getCollectionsByResource(string $resourceType, string $resourceI
173178 return new DataResponse ([], Http::STATUS_NOT_FOUND );
174179 }
175180
176- return new DataResponse (array_map ([ $ this , ' prepareCollection ' ], $ resource ->getCollections ()));
181+ return new DataResponse ($ this -> prepareCollections ( $ resource ->getCollections ()));
177182 }
178183
179184 /**
@@ -202,7 +207,7 @@ public function createCollectionOnResource(string $baseResourceType, string $bas
202207 $ collection = $ this ->manager ->newCollection ($ name );
203208 $ collection ->addResource ($ resource );
204209
205- return new DataResponse ( $ this ->prepareCollection ($ collection) );
210+ return $ this ->respondCollection ($ collection );
206211 }
207212
208213 /**
@@ -221,24 +226,65 @@ public function renameCollection(int $collectionId, string $collectionName): Dat
221226
222227 $ collection ->setName ($ collectionName );
223228
224- return new DataResponse ($ this ->prepareCollection ($ collection ));
229+ return $ this ->respondCollection ($ collection );
230+ }
231+
232+ protected function respondCollection (ICollection $ collection ): DataResponse {
233+ try {
234+ return new DataResponse ($ this ->prepareCollection ($ collection ));
235+ } catch (CollectionException $ e ) {
236+ return new DataResponse ([], Http::STATUS_NOT_FOUND );
237+ } catch (Exception $ e ) {
238+ $ this ->logger ->logException ($ e );
239+ return new DataResponse ([], Http::STATUS_INTERNAL_SERVER_ERROR );
240+ }
241+ }
242+
243+ protected function prepareCollections (array $ collections ): array {
244+ $ result = [];
245+
246+ foreach ($ collections as $ collection ) {
247+ try {
248+ $ result [] = $ this ->prepareCollection ($ collection );
249+ } catch (CollectionException $ e ) {
250+ } catch (Exception $ e ) {
251+ $ this ->logger ->logException ($ e );
252+ }
253+ }
254+
255+ return $ result ;
225256 }
226257
227258 protected function prepareCollection (ICollection $ collection ): array {
228259 if (!$ collection ->canAccess ($ this ->userSession ->getUser ())) {
229- return null ;
260+ throw new CollectionException ( ' Can not access collection ' ) ;
230261 }
231262
232263 return [
233264 'id ' => $ collection ->getId (),
234265 'name ' => $ collection ->getName (),
235- 'resources ' => array_values ( array_filter ( array_map ([ $ this , ' prepareResources ' ], $ collection ->getResources ()) )),
266+ 'resources ' => $ this -> prepareResources ( $ collection ->getResources ()),
236267 ];
237268 }
238269
239- protected function prepareResources (IResource $ resource ): ?array {
270+ protected function prepareResources (array $ resources ): ?array {
271+ $ result = [];
272+
273+ foreach ($ resources as $ resource ) {
274+ try {
275+ $ result [] = $ this ->prepareResource ($ resource );
276+ } catch (ResourceException $ e ) {
277+ } catch (Exception $ e ) {
278+ $ this ->logger ->logException ($ e );
279+ }
280+ }
281+
282+ return $ result ;
283+ }
284+
285+ protected function prepareResource (IResource $ resource ): array {
240286 if (!$ resource ->canAccess ($ this ->userSession ->getUser ())) {
241- return null ;
287+ throw new ResourceException ( ' Can not access resource ' ) ;
242288 }
243289
244290 return $ resource ->getRichObject ();
0 commit comments