Skip to content

Commit b03f5b8

Browse files
committed
Created matter
1 parent 70b3908 commit b03f5b8

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

src/Client/ApiClient.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,55 @@ public function matterByMatterNumber(string $matterNumber): Matter
142142
return $this->createItemResult($response, Matter::class);
143143
}
144144

145+
public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
146+
{
147+
$linkName = 'http://cbrain.com/casefile/rel/create-matter';
148+
$url = null !== $case ? $case->links->getLinkUrl($linkName) : $this->getRequestUrl($linkName);
149+
150+
// The documentation is unclear on this POE stuff. Does it return 303 or 201?
151+
// resources/f2-rest-docs/f2-rest-docs-v13s.html#11
152+
$response = $this->request(Request::METHOD_POST, $url);
153+
if (Response::HTTP_CREATED !== $response->getStatusCode()) {
154+
$message = null !== $case
155+
? sprintf('Cannot get matter create POE URL for case %s', $case)
156+
: 'Cannot get matter create POE URLs';
157+
throw new RuntimeException($message);
158+
}
159+
try {
160+
$location = $this->getHeader('location', $response);
161+
} catch (\Exception $e) {
162+
$message = null !== $case
163+
? sprintf('Cannot get matter create POE URL for case %s', $case)
164+
: 'Cannot get matter create POE URLs';
165+
throw new RuntimeException($message, previous: $e);
166+
}
167+
168+
$response = $this->request(Request::METHOD_POST, $location, [
169+
'json' => $matterData,
170+
]);
171+
172+
if (Response::HTTP_CREATED !== $response->getStatusCode()) {
173+
$message = null !== $case ? sprintf('Cannot create matter for case %s', $case) : 'Cannot create matter';
174+
throw new RuntimeException($message);
175+
}
176+
177+
return $this->createItemResult($response, Matter::class);
178+
}
179+
180+
/**
181+
* Get header value.
182+
*/
183+
private function getHeader(string $name, ResponseInterface $response): string
184+
{
185+
$headers = $response->getHeaders();
186+
$name = strtolower($name);
187+
if (!array_key_exists($name, $headers)) {
188+
throw new RuntimeException(sprintf('Header "%s" not found.', $name));
189+
}
190+
191+
return reset($headers[$name]);
192+
}
193+
145194
public function documentById(int $id): Document
146195
{
147196
$url = $this->getRequestUrl('http://cbrain.com/casefile/rel/document-by-id', [

src/Model/F2Item/Links.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace ItkDev\F2ApiClient\Model\F2Item;
66

7+
use ItkDev\F2ApiClient\Exception\RuntimeException;
8+
79
/**
810
* @phpstan-type Link array{rel: string, href: string, title:string}
911
*/
@@ -16,7 +18,7 @@ public function __construct(
1618
{
1719
}
1820

19-
static function fromSimpleXMLElement(\SimpleXMLElement $sxe): self
21+
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): self
2022
{
2123
$links = [];
2224
foreach ($sxe->Link as $link) {
@@ -30,12 +32,25 @@ static function fromSimpleXMLElement(\SimpleXMLElement $sxe): self
3032
}
3133

3234
/**
33-
* @param string $rel
3435
* @return Link
3536
*/
36-
public function getLink(string $rel): ?array
37+
public function getLink(string $rel): array
38+
{
39+
if (!array_key_exists($rel, $this->links)) {
40+
throw new RuntimeException(sprintf('Cannot get link "%s"', $rel));
41+
}
42+
43+
return $this->links[$rel];
44+
}
45+
46+
public function getLinkUrl(string $rel): string
3747
{
38-
return $this->links[$rel] ?? null;
48+
$link = $this->getLink($rel);
49+
if (!array_key_exists('href', $link) || !is_string($link['href'])) {
50+
throw new RuntimeException(sprintf('Cannot get URL for link "%s"', $rel));
51+
}
52+
53+
return $link['href'];
3954
}
4055

4156
#[\Override]

src/Model/Matter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66

77
final class Matter extends F2Item
88
{
9+
// resources/f2-rest-docs/f2-rest-docs-v13s.html#65
10+
public const string TYPE_INTERNAL = 'Internal';
11+
public const string TYPE_INBOUND = 'Inbound';
12+
public const string TYPE_OUTBOUND= 'Outbound';
13+
914
public string $matterNumber;
1015
public string $title;
1116
public \DateTimeImmutable $createdDate;
1217
public \DateTimeImmutable $modifiedDate;
18+
public PartyItem $createdBy;
1319
public PartyItem $modifiedBy;
14-
public PartyItem $responsible;
20+
public ?PartyItem $responsible;
1521

1622
#[\Override]
1723
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
@@ -23,8 +29,9 @@ public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
2329
$this->title = (string) $sxe->Title;
2430
$this->createdDate = new \DateTimeImmutable((string) $sxe->CreatedDate);
2531
$this->modifiedDate = new \DateTimeImmutable((string) $sxe->ModifiedDate);
32+
$this->createdBy = PartyItem::fromSimpleXMLElement($sxe->CreatedBy);
2633
$this->modifiedBy = PartyItem::fromSimpleXMLElement($sxe->ModifiedBy);
27-
$this->responsible = PartyItem::fromSimpleXMLElement($sxe->Responsible);
34+
$this->responsible = $sxe->Responsible ? PartyItem::fromSimpleXMLElement($sxe->Responsible) : null;
2835

2936
return $this;
3037
}
@@ -43,8 +50,9 @@ public function jsonSerialize(): array
4350
'title' => $this->title,
4451
'createdDate' => $this->createdDate,
4552
'modifiedDate' => $this->modifiedDate,
53+
'createdBy' => $this->createdBy->jsonSerialize(),
4654
'modifiedBy' => $this->modifiedBy->jsonSerialize(),
47-
'responsible' => $this->responsible->jsonSerialize(),
55+
'responsible' => $this->responsible?->jsonSerialize(),
4856
] + parent::jsonSerialize();
4957
}
5058
}

0 commit comments

Comments
 (0)