Skip to content

Commit

Permalink
updated Persoo article feed to Luigi's Box article feed
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Feb 12, 2024
1 parent 2ea43f9 commit b21cc9b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/LuigisBoxArticleFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Shopsys\ArticleFeed\LuigisBoxBundle;

use Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFacade;
use Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFeedItemFacade;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\Feed\FeedInfoInterface;
use Shopsys\FrameworkBundle\Model\Feed\FeedInterface;
Expand All @@ -13,11 +13,11 @@ class LuigisBoxArticleFeed implements FeedInterface
{
/**
* @param \Shopsys\ArticleFeed\LuigisBoxBundle\LuigisBoxArticleFeedInfo $luigisBoxArticleFeedInfo
* @param \Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFacade $luigisBoxArticleFacade
* @param \Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFeedItemFacade $luigisBoxArticleFeedItemFacade
*/
public function __construct(
protected readonly LuigisBoxArticleFeedInfo $luigisBoxArticleFeedInfo,
protected readonly LuigisBoxArticleFacade $luigisBoxArticleFacade,
protected readonly LuigisBoxArticleFeedItemFacade $luigisBoxArticleFeedItemFacade,
) {
}

Expand All @@ -42,6 +42,6 @@ public function getTemplateFilepath(): string
*/
public function getItems(DomainConfig $domainConfig, ?int $lastSeekId, int $maxResults): iterable
{
yield from $this->luigisBoxArticleFacade->getItems($domainConfig, $lastSeekId, $maxResults);
yield from $this->luigisBoxArticleFeedItemFacade->getItems($domainConfig, $lastSeekId, $maxResults);
}
}
52 changes: 47 additions & 5 deletions src/Model/LuigisBoxArticleFeedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ class LuigisBoxArticleFeedItem implements FeedItemInterface
{
/**
* @param int $id
* @param string $index
* @param string $title
* @param string $link
* @param string|null $description
* @param string|null $imageLink
* @param string|null $perex
*/
public function __construct(
protected readonly int $id,
public readonly string $title,
public readonly string $link,
public readonly ?string $description,
public readonly ?string $imageLink,
protected readonly string $index,
protected readonly string $title,
protected readonly string $link,
protected readonly ?string $description,
protected readonly ?string $perex,
) {
}

Expand All @@ -31,4 +33,44 @@ public function getSeekId(): int
{
return $this->id;
}

/**
* @return string
*/
public function getIdentity(): string
{
return $this->index . '-' . $this->id;
}

/**
* @return string
*/
public function getName(): string
{
return $this->title;
}

/**
* @return string
*/
public function getUrl(): string
{
return $this->link;
}

/**
* @return string|null
*/
public function getText(): ?string
{
return $this->description;
}

/**
* @return string|null
*/
public function getAnnotation(): ?string
{
return $this->perex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\CombinedArticle\CombinedArticleElasticsearchFacade;

class LuigisBoxArticleFacade
class LuigisBoxArticleFeedItemFacade
{
/**
* @param \Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFeedItemFactory $luigisBoxArticleFeedItemFactory
Expand All @@ -33,8 +33,7 @@ public function getItems(DomainConfig $domainConfig, ?int $lastSeekId, int $maxR
$articlesData = $this->combinedArticleElasticsearchFacade->getArticlesByDomainId($domainConfig->getId(), $lastSeekId, $maxResults);

foreach ($articlesData as $article) {
yield $this->luigisBoxArticleFeedItemFactory->create($article, $lastSeekId);
$lastSeekId++;
yield $this->luigisBoxArticleFeedItemFactory->create($article);
}
}
}
22 changes: 4 additions & 18 deletions src/Model/LuigisBoxArticleFeedItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,22 @@
namespace Shopsys\ArticleFeed\LuigisBoxBundle\Model;

use Shopsys\FrameworkBundle\Component\String\TransformString;
use Shopsys\FrameworkBundle\Component\Image\ImageUrlWithSizeHelper;

class LuigisBoxArticleFeedItemFactory
{
/**
* @param array $articleData
* @param int $itemNumber
* @return \Shopsys\ArticleFeed\LuigisBoxBundle\Model\LuigisBoxArticleFeedItem
*/
public function create(array $articleData, int $itemNumber): LuigisBoxArticleFeedItem
public function create(array $articleData): LuigisBoxArticleFeedItem
{
return new LuigisBoxArticleFeedItem(
$itemNumber,
$articleData['id'],
$articleData['index'],
$articleData['name'],
$articleData['url'],
TransformString::convertHtmlToPlainText($articleData['text']),
$this->getImageUrl($articleData),
TransformString::convertHtmlToPlainText($articleData['perex'] ?? null),
);
}

/**
* @param array $articleData
* @return string|null
*/
protected function getImageUrl(array $articleData): ?string
{
if (!array_key_exists('imageUrl', $articleData) || $articleData['imageUrl'] === null) {
return null;
}

return ImageUrlWithSizeHelper::limitWidthInImageUrl($articleData['imageUrl']);
}
}
13 changes: 7 additions & 6 deletions src/Resources/views/feed.xml.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

{% block item %}
<article>
<title>{{ item.title }}</title>
<link>{{ item.link }}</link>
{% if item.description is not null %}
<description><![CDATA[{{ item.description }}]]></description>
<identity>{{ item.identity }}</identity>
<name>{{ item.name }}</name>
{% if item.annotation is not null %}
<annotation>{{ item.annotation }}</annotation>
{% endif %}
{% if item.imageLink is not null %}
<imageLink>{{ item.imageLink }}</imageLink>
<url>{{ item.url }}</url>
{% if item.text is not null %}
<text>{{ item.text }}</text>
{% endif %}
</article>
{% endblock %}
Expand Down
27 changes: 15 additions & 12 deletions tests/Unit/LuigisBoxArticleFeedItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ class LuigisBoxArticleFeedItemTest extends TestCase
/**
* @dataProvider articleFeedItemCreationDataProvider
* @param array $articleData
* @param int $itemNumber
* @param string|null $expectedImageUrl
*/
public function testArticleFeedItemCreation(array $articleData, int $itemNumber, ?string $expectedImageUrl): void
public function testArticleFeedItemCreation(array $articleData): void
{
$luigisBoxArticleFeedItemFactory = new LuigisBoxArticleFeedItemFactory();
$luigisBoxArticleFeedItem = $luigisBoxArticleFeedItemFactory->create($articleData, $itemNumber);
$luigisBoxArticleFeedItem = $luigisBoxArticleFeedItemFactory->create($articleData);

$this->assertSame($itemNumber, $luigisBoxArticleFeedItem->getSeekId());
$this->assertSame($articleData['name'], $luigisBoxArticleFeedItem->title);
$this->assertSame($articleData['url'], $luigisBoxArticleFeedItem->link);
$this->assertSame($articleData['text'], $luigisBoxArticleFeedItem->description);
$this->assertSame($expectedImageUrl, $luigisBoxArticleFeedItem->imageLink);
$this->assertSame($articleData['index'] . '-' . $articleData['id'], $luigisBoxArticleFeedItem->getIdentity());
$this->assertSame($articleData['name'], $luigisBoxArticleFeedItem->getName());
$this->assertSame($articleData['url'], $luigisBoxArticleFeedItem->getUrl());
$this->assertSame($articleData['text'], $luigisBoxArticleFeedItem->getText());
}

/**
Expand All @@ -45,15 +42,18 @@ public function articleFeedItemCreationDataProvider(): iterable

yield [
'articleData' => [
'id' => 1,
'index' => 'article',
...$commonArticleData,
'imageUrl' => self::ARTICLE_IMAGE_URL,
],
'itemNumber' => 1,
'expectedImageUrl' => self::ARTICLE_IMAGE_URL . '?width=605',
];

yield [
'articleData' => [
'id' => 2,
'index' => 'article',
...$commonArticleData,
'imageUrl' => null,
],
Expand All @@ -62,8 +62,11 @@ public function articleFeedItemCreationDataProvider(): iterable
];

yield [
'articleData' => $commonArticleData,
'itemNumber' => 3,
'articleData' => [
'id' => 1,
'index' => 'blog_article',
...$commonArticleData,
],
'expectedImageUrl' => null,
];
}
Expand Down

0 comments on commit b21cc9b

Please sign in to comment.