66
77use ItkDev \F2ApiClient \Exception \ApiException ;
88use ItkDev \F2ApiClient \Exception \RuntimeException ;
9+ use ItkDev \F2ApiClient \Model \AbstractF2Item ;
910use ItkDev \F2ApiClient \Model \AbstractItem ;
1011use ItkDev \F2ApiClient \Model \Atom ;
1112use ItkDev \F2ApiClient \Model \CaseFile ;
13+ use ItkDev \F2ApiClient \Model \Collection ;
1214use ItkDev \F2ApiClient \Model \Document ;
1315use ItkDev \F2ApiClient \Model \Matter ;
1416use Psr \Cache \CacheItemInterface ;
1517use Psr \Cache \CacheItemPoolInterface ;
1618use Psr \Log \LoggerAwareTrait ;
1719use Psr \Log \LoggerTrait ;
20+ use Swaggest \JsonDiff \JsonDiff ;
1821use Symfony \Component \Cache \Adapter \ProxyAdapter ;
1922use Symfony \Component \HttpClient \HttpClient ;
2023use Symfony \Component \HttpFoundation \Request ;
@@ -163,6 +166,27 @@ public function caseCreate(array $caseData): CaseFile
163166 return $ this ->createItemResult ($ response , CaseFile::class);
164167 }
165168
169+ public function caseFileUpdate (CaseFile $ caseFile , CaseFile $ updatedCaseFile ): bool
170+ {
171+ $ url = $ caseFile ->links ->getLinkUrl ('self ' );
172+ $ diff = new JsonDiff (
173+ $ caseFile ->jsonSerialize (),
174+ $ updatedCaseFile ->jsonSerialize (),
175+ // The F2 API does not support "test" in JSON Patch
176+ options: JsonDiff::SKIP_TEST_OPS ,
177+ );
178+ $ response = $ this ->request (Request::METHOD_PATCH , $ url , [
179+ 'json ' => $ diff ->getPatch ()->jsonSerialize (),
180+ ]);
181+
182+ if (Response::HTTP_OK !== $ response ->getStatusCode ()) {
183+ $ message = sprintf ('Cannot update case %s ' , $ caseFile );
184+ throw $ this ->createRuntimeException ($ message , response: $ response );
185+ }
186+
187+ return true ;
188+ }
189+
166190 /**
167191 * @return Atom[]
168192 */
@@ -244,6 +268,26 @@ public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
244268 return $ this ->createItemResult ($ response , Matter::class);
245269 }
246270
271+ public function matterUpdate (Matter $ matter , Matter $ updatedMatter ): bool
272+ {
273+ $ url = $ matter ->links ->getLinkUrl ('self ' );
274+ $ diff = new JsonDiff (
275+ $ matter ->jsonSerialize (),
276+ $ updatedMatter ->jsonSerialize (),
277+ options: JsonDiff::SKIP_TEST_OPS ,
278+ );
279+ $ response = $ this ->request (Request::METHOD_PATCH , $ url , [
280+ 'json ' => $ diff ->getPatch ()->jsonSerialize (),
281+ ]);
282+
283+ if (Response::HTTP_OK !== $ response ->getStatusCode ()) {
284+ $ message = sprintf ('Cannot update matter %s ' , $ matter );
285+ throw $ this ->createRuntimeException ($ message , response: $ response );
286+ }
287+
288+ return true ;
289+ }
290+
247291 public function documentById (int $ id ): Document
248292 {
249293 $ url = $ this ->getRequestUrl ('http://cbrain.com/casefile/rel/document-by-id ' , [
@@ -258,6 +302,18 @@ public function documentById(int $id): Document
258302 return $ this ->createItemResult ($ response , Document::class);
259303 }
260304
305+ public function getDocumentContent (Document $ document ): ResponseInterface
306+ {
307+ $ url = $ document ->links ->getLinkUrl ('http://cbrain.com/casefile/rel/content ' );
308+ $ response = $ this ->request (Request::METHOD_GET , $ url );
309+
310+ if (Response::HTTP_OK !== $ response ->getStatusCode ()) {
311+ throw $ this ->createApiException ($ response );
312+ }
313+
314+ return $ response ;
315+ }
316+
261317 public function documentCreate (string $ filename , array $ documentData , Matter $ matter ): Document
262318 {
263319 $ linkName = 'http://cbrain.com/casefile/rel/create-document ' ;
@@ -337,6 +393,13 @@ protected function getAccessToken(): array
337393
338394 protected function request (string $ method , string $ path , array $ options = []): ResponseInterface
339395 {
396+ if (Request::METHOD_PATCH === $ method && array_key_exists ('json ' , $ options )) {
397+ if (!array_key_exists ('headers ' , $ options ) || !is_array ($ options ['headers ' ])) {
398+ $ options ['headers ' ] = [];
399+ }
400+ $ options ['headers ' ]['content-type ' ] = 'application/json-patch ' ;
401+ }
402+
340403 $ accessToken = $ this ->getAccessToken ();
341404
342405 try {
@@ -555,4 +618,14 @@ private function logException(RuntimeException $exception): RuntimeException
555618
556619 return $ exception ;
557620 }
621+
622+ public function getDown (AbstractF2Item $ item ): Collection
623+ {
624+ $ url = $ item ->links ->getLinkUrl ('down ' );
625+ $ response = $ this ->request (Request::METHOD_GET , $ url );
626+
627+ $ sxe = new \SimpleXMLElement ($ response ->getContent ());
628+
629+ return Collection::fromSimpleXMLElement ($ sxe );
630+ }
558631}
0 commit comments