Files
newspage/classes/Application/Content/CockpitApi.php
2024-11-07 21:34:32 +01:00

140 lines
3.6 KiB
PHP

<?php
namespace Application\Content;
use Application\AppEnvironment;
use Application\Models\Article;
use Application\Models\Site;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use JMS\Serializer\SerializerBuilder;
class CockpitApi
{
private ?Client $client = null;
public function __construct()
{
$this->client = new Client();
}
/**
* @param string $type
* @param array $variables
*
* @return string
* @throws Exception
*/
private function getApiPath(string $type = 'item', array $variables = []): string
{
$path = match ($type) {
'itemByModel' => '/content/item/{model}',
'itemsByModel' => '/content/items/{model}',
'itemByModelAndId' => '/content/item/{model}/{id}',
'items' => '/content/items',
'aggregateByModel' => '/content/aggregate/{model}',
'treeByModel' => '/content/tree/{model}',
'healthcheck' => '/system/healthcheck',
default => throw new Exception("Type '$type' does not exist."),
};
foreach ($variables as $search => $replace) {
$path = str_replace('{' . $search . '}', $replace, $path);
}
if (str_contains($path, '{')) {
throw new Exception("Non replaced variable in path '$path'.");
}
return AppEnvironment::getInstance()->getVariable('COCKPIT_API_PATH') . $path;
}
/**
* @throws GuzzleException
* @throws Exception
*/
public function getGlobalData(): Site
{
$res = $this->client->request('GET', $this->getApiPath('itemByModel', ['model' => 'site']), [
'headers' => [
'api-key' => AppEnvironment::getInstance()->getVariable('COCKPIT_API_KEY'),
],
]);
return $this->deserialize($res->getBody()->getContents(), Site::class);
}
/**
* @throws GuzzleException
* @throws Exception
* @return Article[]
*/
public function getArticles(): array
{
$res = $this->client->request('GET', $this->getApiPath('itemsByModel', ['model' => 'article']), [
'headers' => [
'api-key' => AppEnvironment::getInstance()->getVariable('COCKPIT_API_KEY'),
],
'query' => [
'filter' => null, # '{fieldA:"test"}'
'fields' => null,
'sort' => null,
'limit' => 10,
'skip' => 0,
'populate' => 1,
],
]);
return $this->deserialize($res->getBody()->getContents(), Article::class);
}
/**
* @throws GuzzleException
* @throws Exception
*/
public function getArticle(string $id): Article
{
$res = $this->client->request('GET', $this->getApiPath('itemByModelAndId', ['model' => 'article', 'id' => $id]), [
'headers' => [
'api-key' => AppEnvironment::getInstance()->getVariable('COCKPIT_API_KEY'),
],
'query' => [
'filter' => null, # "{_id:$id}",
'fields' => null,
'sort' => null,
'limit' => 1,
'skip' => 0,
'populate' => 1,
],
]);
return $this->deserialize($res->getBody()->getContents(), Article::class);
}
/**
* Deserialize JSON data into objects.
*
* @param string $json
* @param string $type
*
* @return mixed
*/
private function deserialize(string $json, string $type): mixed
{
$array = json_decode($json, true);
if (!isset($array['data'])) {
return SerializerBuilder::create()->build()->deserialize($json, $type, 'json');
}
$return = [];
foreach ($array['data'] as $data) {
$json = json_encode($data);
$return[] = SerializerBuilder::create()->build()->deserialize($json, $type, 'json');
}
return $return;
}
}