Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ build:
analysis:
environment:
php:
version: 8.3.3
version: 8.3.26
tests:
override:
- php-scrutinizer-run
129 changes: 111 additions & 18 deletions src/BEditaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,56 @@ public function authenticate(string $username, string $password): ?array
return $this->post('/auth', $body, ['Content-Type' => 'application/json']);
}

/**
* Bulk edit objects using `POST /bulk/edit` endpoint.
* If the endpoint is not available, it fallback to edit one by one (retrocompatible way).
*
* $objects is an array of <type>: <array of ids>, e.g.:
* [
* 'articles' => [1,2,3],
* 'documents' => [4,5,6],
* ]
*
* The $attributes is an array of attributes to modify, e.g.:
* [
* 'title' => 'New title',
* 'status' => 'off',
* ]
*
* @param array $objects Object data to indentify objects to edit
* @param array $attributes Data to modify
* @return array
*/
public function bulkEdit(array $objects, array $attributes): array
{
$result = ['data' => ['saved' => [], 'errors' => []]];
try {
$result = (array)$this->post(
'/bulk/edit',
json_encode(['data' => compact('attributes', 'objects')]),
['Content-Type' => 'application/json'],
);
} catch (Exception $e) {
// fallback to edit one by one, to be retrocompatible
$types = array_keys($objects);
foreach ($types as $type) {
foreach ($objects[$type] as $id) {
try {
$response = $this->save($type, $attributes + ['id' => (string)$id]);
$result['data']['saved'][] = $response['data']['id'];
} catch (Exception $e) {
$result['data']['errors'][] = [
'id' => $id,
'message' => $e->getMessage(),
];
}
}
}
}

return $result;
}

/**
* GET a list of resources or objects of a given type
*
Expand All @@ -61,8 +111,12 @@ public function getObjects(string $type = 'objects', ?array $query = null, ?arra
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function getObject(string|int $id, string $type = 'objects', ?array $query = null, ?array $headers = null): ?array
{
public function getObject(
string|int $id,
string $type = 'objects',
?array $query = null,
?array $headers = null,
): ?array {
return $this->get(sprintf('/%s/%s', $type, $id), $query, $headers);
}

Expand All @@ -76,8 +130,13 @@ public function getObject(string|int $id, string $type = 'objects', ?array $quer
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function getRelated(string|int $id, string $type, string $relation, ?array $query = null, ?array $headers = null): ?array
{
public function getRelated(
string|int $id,
string $type,
string $relation,
?array $query = null,
?array $headers = null,
): ?array {
return $this->get(sprintf('/%s/%s/%s', $type, $id, $relation), $query, $headers);
}

Expand All @@ -91,9 +150,18 @@ public function getRelated(string|int $id, string $type, string $relation, ?arra
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function addRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->post(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
public function addRelated(
string|int $id,
string $type,
string $relation,
array $data,
?array $headers = null,
): ?array {
return $this->post(
sprintf('/%s/%s/relationships/%s', $type, $id, $relation),
json_encode(compact('data')),
$headers,
);
}

/**
Expand All @@ -106,9 +174,18 @@ public function addRelated(string|int $id, string $type, string $relation, array
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function removeRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->delete(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
public function removeRelated(
string|int $id,
string $type,
string $relation,
array $data,
?array $headers = null,
): ?array {
return $this->delete(
sprintf('/%s/%s/relationships/%s', $type, $id, $relation),
json_encode(compact('data')),
$headers,
);
}

/**
Expand All @@ -121,9 +198,23 @@ public function removeRelated(string|int $id, string $type, string $relation, ar
* @param array|null $headers Custom request headers
* @return array|null Response in array format
*/
public function replaceRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
{
return $this->patch(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
public function replaceRelated(
string|int $id,
string $type,
string $relation,
array $data,
?array $headers = null,
): ?array {
return $this->patch(
sprintf(
'/%s/%s/relationships/%s',
$type,
$id,
$relation,
),
json_encode(compact('data')),
$headers,
);
}

/**
Expand Down Expand Up @@ -318,10 +409,12 @@ public function addStreamToMedia(string $streamId, string $id, string $type): vo
'id' => $id,
'type' => $type,
],
])
]),
);
if (empty($response)) {
throw new BEditaClientException('Invalid response from PATCH ' . sprintf('/streams/%s/relationships/object', $id));
throw new BEditaClientException(
'Invalid response from PATCH ' . sprintf('/streams/%s/relationships/object', $id),
);
}
}

Expand Down Expand Up @@ -360,7 +453,7 @@ public function schema(string $type): ?array
return $this->get(
sprintf('/model/schema/%s', $type),
null,
['Accept' => 'application/schema+json']
['Accept' => 'application/schema+json'],
);
}

Expand All @@ -374,7 +467,7 @@ public function relationData(string $name): ?array
{
return $this->get(
sprintf('/model/relations/%s', $name),
['include' => 'left_object_types,right_object_types']
['include' => 'left_object_types,right_object_types'],
);
}

Expand All @@ -394,7 +487,7 @@ public function restoreObject(string|int $id, string $type): ?array
'id' => $id,
'type' => $type,
],
])
]),
);
}

Expand Down
27 changes: 21 additions & 6 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@ public function refreshTokens(): void
* @param \Psr\Http\Message\StreamInterface|resource|string|null $body Request body.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function sendRequestRetry(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null): ResponseInterface
{
protected function sendRequestRetry(
string $method,
string $path,
?array $query = null,
?array $headers = null,
$body = null,
): ResponseInterface {
try {
return $this->sendRequest($method, $path, $query, $headers, $body);
} catch (BEditaClientException $e) {
Expand Down Expand Up @@ -275,8 +280,13 @@ protected function sendRequestRetry(string $method, string $path, ?array $query
* @param \Psr\Http\Message\StreamInterface|resource|string|null $body Request body.
* @return \Psr\Http\Message\ResponseInterface
*/
protected function refreshAndRetry(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null): ResponseInterface
{
protected function refreshAndRetry(
string $method,
string $path,
?array $query = null,
?array $headers = null,
$body = null,
): ResponseInterface {
$this->refreshTokens();
unset($headers['Authorization']);

Expand All @@ -294,8 +304,13 @@ protected function refreshAndRetry(string $method, string $path, ?array $query =
* @return \Psr\Http\Message\ResponseInterface
* @throws \BEdita\SDK\BEditaClientException Throws an exception if server response code is not 20x.
*/
protected function sendRequest(string $method, string $path, ?array $query = null, ?array $headers = null, $body = null): ResponseInterface
{
protected function sendRequest(
string $method,
string $path,
?array $query = null,
?array $headers = null,
$body = null,
): ResponseInterface {
$uri = $this->requestUri($path, $query);
$headers = array_merge($this->defaultHeaders, (array)$headers);

Expand Down
4 changes: 2 additions & 2 deletions src/LogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function logRequest(RequestInterface $request): void
$request->getMethod(),
$request->getUri(),
$this->requestHeadersCleanup($request),
$this->requestBodyCleanup($request)
$this->requestBodyCleanup($request),
);
$this->logger->info($msg);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public function logResponse(ResponseInterface $response): void
$response->getStatusCode(),
$response->getReasonPhrase(),
json_encode($response->getHeaders()),
$this->responseBodyCleanup($response)
$this->responseBodyCleanup($response),
);
$this->logger->info($msg);
}
Expand Down
Loading