From a4b336b3fddc1078ff1711e3efe0d865e808f151 Mon Sep 17 00:00:00 2001 From: Adrian Rauth Date: Thu, 7 Nov 2024 21:34:32 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Cockpit=20integration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/phpunit.xml | 2 +- classes/Application/Content/CockpitApi.php | 4 +- .../Controller/ArticleController.php | 18 +++-- .../Application/Controller/HomeController.php | 18 +++-- .../Application/Controller/MainController.php | 13 +--- classes/Application/Models/Article.php | 75 ++++++++++++++++--- classes/Application/Models/Image.php | 62 ++++++++++++++- templates/03_organisms/o-card/o-card.twig | 2 +- 8 files changed, 153 insertions(+), 41 deletions(-) diff --git a/.idea/phpunit.xml b/.idea/phpunit.xml index d46ece4..115fc42 100644 --- a/.idea/phpunit.xml +++ b/.idea/phpunit.xml @@ -9,8 +9,8 @@ diff --git a/classes/Application/Content/CockpitApi.php b/classes/Application/Content/CockpitApi.php index 2365bfd..c351f88 100644 --- a/classes/Application/Content/CockpitApi.php +++ b/classes/Application/Content/CockpitApi.php @@ -68,8 +68,9 @@ class CockpitApi /** * @throws GuzzleException * @throws Exception + * @return Article[] */ - public function getArticles(): Article|array + public function getArticles(): array { $res = $this->client->request('GET', $this->getApiPath('itemsByModel', ['model' => 'article']), [ 'headers' => [ @@ -130,7 +131,6 @@ class CockpitApi $return = []; foreach ($array['data'] as $data) { $json = json_encode($data); - dump($json); $return[] = SerializerBuilder::create()->build()->deserialize($json, $type, 'json'); } diff --git a/classes/Application/Controller/ArticleController.php b/classes/Application/Controller/ArticleController.php index d65c47c..e2ebf47 100644 --- a/classes/Application/Controller/ArticleController.php +++ b/classes/Application/Controller/ArticleController.php @@ -1,5 +1,7 @@ getArticle($this->getRouteParams('id')); + $this->getBreadcrumbs()->addBreadcrumb( + $article->getTitle(), + $article->getUrl() + ); + $this->pushToStash('article', [ - 'subtitle' => $this->faker->sentence(2), + 'subtitle' => 'TODO: Subtitle', 'title' => $article->getTitle(), - 'lead' => $this->faker->paragraph(), + 'lead' => $article->getExcerpt(), 'text' => $article->getText(), 'image' => [ - 'src' => 'https://picsum.photos/seed/' . $this->faker->randomDigit() . '/1920/1080', - 'width' => 1920, - 'height' => 1080, + 'src' => $article->getImage()->getImageUrl(), + 'width' => $article->getImage()->getWidth(), + 'height' => $article->getImage()->getHeight(), 'alt' => 'lorem ipsum', ], ]); diff --git a/classes/Application/Controller/HomeController.php b/classes/Application/Controller/HomeController.php index 62eb6f0..21df67b 100644 --- a/classes/Application/Controller/HomeController.php +++ b/classes/Application/Controller/HomeController.php @@ -1,27 +1,33 @@ pushToStash('articles', $this->generateArticles()); + $this->pushToStash('articles', $this->getArticles()); } - private function generateArticles(): array + /** + * @throws GuzzleException + */ + private function getArticles(): array { $return = []; - for ($i = 0; $i < 10; $i++) { - $return[] = (new Article(NumberHash::numHash("home_$i")))->getTeaserData(); + foreach ((new CockpitApi())->getArticles() as $article) { + $return[] = $article->getTeaserData(); } return $return; diff --git a/classes/Application/Controller/MainController.php b/classes/Application/Controller/MainController.php index ccc4803..22ded63 100644 --- a/classes/Application/Controller/MainController.php +++ b/classes/Application/Controller/MainController.php @@ -10,8 +10,6 @@ use Twig\Error\LoaderError; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; use Twig\Loader\FilesystemLoader; -use Faker\Factory; -use Faker\Generator; abstract class MainController { @@ -50,13 +48,6 @@ abstract class MainController */ private array $route_parameters = array(); - /** - * Fake data generator. - * - * @var Generator - */ - protected Generator $faker; - /** * Breadcrumbs. * @@ -73,8 +64,6 @@ abstract class MainController public function __invoke(array $parameters = array()): void { $this->route_parameters = $parameters; - $this->faker = Factory::create(); - $this->faker->seed(1234567); $this->preAction(); $this->action(); $this->postAction(); @@ -150,7 +139,7 @@ abstract class MainController $this->pushToStash( 'meta', [ - 'title' => $this->faker->domainName(), + 'title' => 'News Page | Your #1 News Source', ], self::STASH_GLOBAL ); diff --git a/classes/Application/Models/Article.php b/classes/Application/Models/Article.php index 1f592f4..fe4b3f0 100644 --- a/classes/Application/Models/Article.php +++ b/classes/Application/Models/Article.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Application\Models; +use Exception; use JMS\Serializer\Annotation\SerializedName; use JMS\Serializer\Annotation\Type; @@ -13,39 +14,95 @@ class Article #[SerializedName('authors')] #[Type('array<' . Author::class . '>')] - protected array $authors; + protected ?array $authors = null; #[SerializedName('headline')] #[Type('string')] - protected string $headline; + protected ?string $headline = null; #[SerializedName('excerpt')] #[Type('string')] - protected string $excerpt; + protected ?string $excerpt = null; #[SerializedName('image')] #[Type(Image::class)] - protected Image $image; + protected ?Image $image = null; #[SerializedName('text')] #[Type('string')] - protected string $text; + protected ?string $text = null; /** * Return the title / headline. * - * @return string + * @return string|null */ - public function getTitle(): string { + public function getTitle(): string|null + { return $this->headline; } /** * Return the text / article body text. * - * @return string + * @return string|null */ - public function getText(): string { + public function getText(): string|null + { return $this->text; } + + /** + * Get the excerpt / teaser text. + * + * @return string|null + */ + public function getExcerpt(): string|null + { + return $this->excerpt; + } + + public function getImage(): Image|null + { + return $this->image; + } + + public function getTeaserData(): array + { + return [ + 'headline' => $this->getHeadline(), + 'kicker' => $this->getKicker(), + 'excerpt' => $this->getExcerpt(), + 'teaserImage' => $this->getTeaserImage(), + 'url' => $this->getUrl(), + ]; + } + + private function getIdentifier(): string + { + return $this->metaId; + } + + private function getHeadline(): ?string + { + return $this->headline; + } + + private function getKicker(): ?string + { + return $this->excerpt; + } + + /** + * @throws Exception + */ + private function getTeaserImage(): array + { + return $this->image->getTeaserData(); + } + + public function getUrl(): string + { + return '/article/test_article-' . $this->getIdentifier(); + } } diff --git a/classes/Application/Models/Image.php b/classes/Application/Models/Image.php index 6e64e81..28d68fb 100644 --- a/classes/Application/Models/Image.php +++ b/classes/Application/Models/Image.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Application\Models; +use Application\AppEnvironment; +use Exception; use JMS\Serializer\Annotation\SerializedName; use JMS\Serializer\Annotation\Type; @@ -44,12 +46,12 @@ class Image private array $colors; #[SerializedName('width')] - #[Type('string')] - private string $width; + #[Type('int')] + private int $width; #[SerializedName('height')] - #[Type('string')] - private string $height; + #[Type('int')] + private int $height; #[SerializedName('altText')] #[Type('string')] @@ -62,4 +64,56 @@ class Image #[SerializedName('folder')] #[Type('string')] private string $folder; + + /** + * Return the URL to the original asset. + * + * @return string + * @throws Exception + */ + public function getImageUrl(): string + { + $apiPath = AppEnvironment::getInstance()->getVariable('COCKPIT_API_PATH'); + $url = parse_url($apiPath, PHP_URL_SCHEME) . '://'; + $url .= parse_url($apiPath, PHP_URL_HOST) . '/storage/uploads'; + $url .= $this->path; + + return $url; + } + + /** + * Return original image width in pixel. + * + * @return int + */ + public function getWidth(): int + { + return $this->width; + } + + /** + * Return original image height in pixel. + * + * @return int + */ + public function getHeight(): int + { + return $this->height; + } + + /** + * Get teaser data array ready to be used within the template. + * + * @return array + * @throws Exception + */ + public function getTeaserData(): array + { + return [ + 'src' => $this->getImageUrl(), + 'width' => $this->getWidth(), + 'height' => $this->getHeight(), + 'alt' => $this->altText, + ]; + } } diff --git a/templates/03_organisms/o-card/o-card.twig b/templates/03_organisms/o-card/o-card.twig index 2888393..cde2022 100644 --- a/templates/03_organisms/o-card/o-card.twig +++ b/templates/03_organisms/o-card/o-card.twig @@ -6,7 +6,7 @@
-

{{ headline }}

+

{{ headline }}

{{ text }}