Skip to content

Commit 3074916

Browse files
committed
More stuff
1 parent 09e634c commit 3074916

5 files changed

Lines changed: 97 additions & 39 deletions

File tree

mago.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ excessive-parameter-list = {
5050

5151
# Our API client has many methods.
5252
too-many-methods = {
53-
threshold = 30
53+
threshold = 32
5454
}
5555

5656
# Our API client is not really that complex …
5757
cyclomatic-complexity = {
58-
threshold = 38
58+
threshold = 44
5959
}
6060

6161
too-many-properties = {
6262
threshold = 11
6363
}
6464

6565
kan-defect = {
66-
threshold = 1.92
66+
threshold = 2.35
6767
}
6868

6969
[analyzer]

src/Client/ApiClient.php

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,10 @@ public function caseCreate(array $caseData): CaseFile
168168

169169
public function caseFileUpdate(CaseFile $caseFile, CaseFile $updatedCaseFile): bool
170170
{
171-
$url = $caseFile->links->getLinkUrl('self');
171+
$url = $caseFile->links->getUrl('self');
172172
$diff = new JsonDiff(
173173
$caseFile->jsonSerialize(),
174174
$updatedCaseFile->jsonSerialize(),
175-
// The F2 API does not support "test" in JSON Patch
176175
options: JsonDiff::SKIP_TEST_OPS,
177176
);
178177
$response = $this->request(Request::METHOD_PATCH, $url, [
@@ -234,7 +233,7 @@ public function matterByMatterNumber(string $matterNumber): Matter
234233
public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
235234
{
236235
$linkName = 'http://cbrain.com/casefile/rel/create-matter';
237-
$url = null !== $case ? $case->links->getLinkUrl($linkName) : $this->getRequestUrl($linkName);
236+
$url = null !== $case ? $case->links->getUrl($linkName) : $this->getRequestUrl($linkName);
238237

239238
// The documentation is unclear on this POE stuff. Does it return 303 or 201?
240239
// resources/f2-rest-docs/f2-rest-docs-v13s.html#11
@@ -270,19 +269,17 @@ public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
270269

271270
public function matterUpdate(Matter $matter, Matter $updatedMatter): bool
272271
{
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-
]);
272+
$url = $matter->links->getUrl('self');
273+
$diff = $this->computeDiff($matter, $updatedMatter);
274+
if ($diff->getDiffCnt() > 0) {
275+
$response = $this->request(Request::METHOD_PATCH, $url, [
276+
'json' => $diff->getPatch()->jsonSerialize(),
277+
]);
282278

283-
if (Response::HTTP_OK !== $response->getStatusCode()) {
284-
$message = sprintf('Cannot update matter %s', $matter);
285-
throw $this->createRuntimeException($message, response: $response);
279+
if (Response::HTTP_OK !== $response->getStatusCode()) {
280+
$message = sprintf('Cannot update matter %s', $matter);
281+
throw $this->createRuntimeException($message, response: $response);
282+
}
286283
}
287284

288285
return true;
@@ -302,22 +299,10 @@ public function documentById(int $id): Document
302299
return $this->createItemResult($response, Document::class);
303300
}
304301

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-
317-
public function documentCreate(string $filename, array $documentData, Matter $matter): Document
302+
public function documentCreate(array $documentData, string $filename, Matter $matter): Document
318303
{
319304
$linkName = 'http://cbrain.com/casefile/rel/create-document';
320-
$url = $matter->links->getLinkUrl($linkName);
305+
$url = $matter->links->getUrl($linkName);
321306

322307
// The documentation is unclear on this POE stuff. Does it return 303 or 201?
323308
// resources/f2-rest-docs/f2-rest-docs-v13s.html#11
@@ -358,6 +343,61 @@ public function documentCreate(string $filename, array $documentData, Matter $ma
358343
return $this->createItemResult($response, Document::class);
359344
}
360345

346+
public function documentUpdate(Document $document, Document $updatedDocument, ?string $filename = null): bool
347+
{
348+
$url = $document->links->getUrl('self');
349+
$diff = $this->computeDiff($document, $updatedDocument);
350+
if ($diff->getDiffCnt() > 0) {
351+
$response = $this->request(Request::METHOD_PATCH, $url, [
352+
'json' => $diff->getPatch()->jsonSerialize(),
353+
]);
354+
355+
if (Response::HTTP_OK !== $response->getStatusCode()) {
356+
$message = sprintf('Cannot update document %s', $document);
357+
throw $this->createRuntimeException($message, response: $response);
358+
}
359+
}
360+
361+
if (null !== $filename) {
362+
$fileHandle = fopen($filename, 'r');
363+
if (false === $fileHandle) {
364+
$message = sprintf('Cannot open document file %s', $filename);
365+
throw $this->createRuntimeException($message);
366+
}
367+
368+
$url = $document->links->getUrl('http://cbrain.com/casefile/rel/content');
369+
370+
try {
371+
$response = $this->request(Request::METHOD_PUT, $url, [
372+
'body' => [
373+
'File' => $fileHandle,
374+
],
375+
]);
376+
} finally {
377+
fclose($fileHandle);
378+
}
379+
380+
if (Response::HTTP_NO_CONTENT !== $response->getStatusCode()) {
381+
$message = sprintf('Cannot update file on document %s', $document);
382+
throw $this->createRuntimeException($message, response: $response);
383+
}
384+
}
385+
386+
return true;
387+
}
388+
389+
public function getDocumentContent(Document $document): ResponseInterface
390+
{
391+
$url = $document->links->getUrl('http://cbrain.com/casefile/rel/content');
392+
$response = $this->request(Request::METHOD_GET, $url);
393+
394+
if (Response::HTTP_OK !== $response->getStatusCode()) {
395+
throw $this->createApiException($response);
396+
}
397+
398+
return $response;
399+
}
400+
361401
/**
362402
* @return AccessToken
363403
*/
@@ -619,13 +659,23 @@ private function logException(RuntimeException $exception): RuntimeException
619659
return $exception;
620660
}
621661

622-
public function getDown(AbstractF2Item $item): Collection
662+
public function getLinkCollection(AbstractF2Item $item, string $rel): Collection
623663
{
624-
$url = $item->links->getLinkUrl('down');
664+
$url = $item->links->getUrl($rel);
625665
$response = $this->request(Request::METHOD_GET, $url);
626666

627667
$sxe = new \SimpleXMLElement($response->getContent());
628668

629669
return Collection::fromSimpleXMLElement($sxe);
630670
}
671+
672+
private function computeDiff(AbstractF2Item $old, AbstractF2Item $new): JsonDiff
673+
{
674+
$oldValues = $old->jsonSerialize();
675+
$newValues = $new->jsonSerialize();
676+
677+
// @todo Filter out some values, e.g. timestamps.
678+
679+
return new JsonDiff($oldValues, $newValues, options: JsonDiff::SKIP_TEST_OPS);
680+
}
631681
}

src/Model/AbstractF2Item.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public function __toString(): string
3232
return sprintf('F2 Item (%s)', $this->id);
3333
}
3434

35+
/**
36+
* Serialize with the names (paths) needed for JSON patch.
37+
*/
3538
#[\Override]
3639
public function jsonSerialize(): array
3740
{

src/Model/Collection/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
2323
#[\Override]
2424
public function __toString(): string
2525
{
26-
return sprintf('Document %s (#%d)', $this->title, $this->id);
26+
return sprintf('%s (#%d)', $this->title, $this->id);
2727
}
2828

2929
#[\Override]

src/Model/F2Item/Links.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,26 @@ public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): static
4141
return new self(links: $links);
4242
}
4343

44+
public function has(string $rel): bool
45+
{
46+
return array_key_exists($rel, $this->links);
47+
}
48+
4449
/**
4550
* @return Link
4651
*/
47-
public function getLink(string $rel): array
52+
public function get(string $rel): array
4853
{
49-
if (!array_key_exists($rel, $this->links)) {
54+
if (!$this->has($rel)) {
5055
throw new RuntimeException(sprintf('Cannot get link "%s"', $rel));
5156
}
5257

5358
return $this->links[$rel];
5459
}
5560

56-
public function getLinkUrl(string $rel): string
61+
public function getUrl(string $rel): string
5762
{
58-
$link = $this->getLink($rel);
63+
$link = $this->get($rel);
5964
if (!array_key_exists('href', $link)) {
6065
throw new RuntimeException(sprintf('Cannot get URL for link "%s"', $rel));
6166
}

0 commit comments

Comments
 (0)