Skip to content

Commit

Permalink
Cleanup code duplication in filters
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Dec 1, 2023
1 parent 70a8c1d commit 2adbe4c
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 92 deletions.
48 changes: 19 additions & 29 deletions src/ChangeFilter/CategoryChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@

namespace FreshAdvance\Sitemap\ChangeFilter;

use DateTime;
use Doctrine\DBAL\Connection;
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\Eshop\Application\Model\Category;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;

class CategoryChangeFilter implements ChangeFilterInterface
class CategoryChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
protected Connection $connection;
public function getObjectType(): string
{
return 'category';
}

public function __construct(
ConnectionProviderInterface $connectionProvider
) {
$this->connection = $connectionProvider->get();
protected function getModelClass(): string
{
return Category::class;
}

public function getObjectType(): string
protected function getChangeFrequency(): string
{
return 'category';
return 'daily';
}

public function getUpdatedUrls(int $limit): iterable
protected function getPriority(): float
{
return 0.7;
}

protected function filterAndQueryItems(int $limit): Result
{
$query = "SELECT c.OXID
FROM oxcategories c
Expand All @@ -43,6 +46,7 @@ public function getUpdatedUrls(int $limit): iterable
ORDER BY c.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
Expand All @@ -51,20 +55,6 @@ public function getUpdatedUrls(int $limit): iterable
]
);

while ($data = $result->fetchAssociative()) {
$item = oxNew(Category::class);
$item->load((string)$data['OXID']); // @phpstan-ignore-line

yield new ObjectUrl(
objectId: $item->getId(),
objectType: $this->getObjectType(),
url: new PageUrl(
location: (string)$item->getLink(),
lastModified: new DateTime($item->getFieldData('oxtimestamp')), // @phpstan-ignore-line
changeFrequency: 'daily',
priority: 0.9
)
);
}
return $result;
}
}
65 changes: 65 additions & 0 deletions src/ChangeFilter/ChangeFilterTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* Copyright © MB Arbatos Klubas. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace FreshAdvance\Sitemap\ChangeFilter;

use DateTime;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ForwardCompatibility\Result;
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use OxidEsales\EshopCommunity\Core\Contract\IUrl;
use OxidEsales\EshopCommunity\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;

abstract class ChangeFilterTemplate
{
protected Connection $connection;

public function __construct(
ConnectionProviderInterface $connectionProvider
) {
$this->connection = $connectionProvider->get();
}

abstract public function getObjectType(): string;

public function getUpdatedUrls(int $limit): iterable
{
$result = $this->filterAndQueryItems($limit);

while ($data = $result->fetchAssociative()) {
/** @var IUrl&BaseModel $item */
$item = oxNew($this->getModelClass());
$item->load((string)$data['OXID']); // @phpstan-ignore-line

yield new ObjectUrl(
objectId: $item->getId(),
objectType: $this->getObjectType(),
url: new PageUrl(
location: (string)$item->getLink(),
lastModified: new DateTime($item->getFieldData('oxtimestamp')), // @phpstan-ignore-line
changeFrequency: $this->getChangeFrequency(),
priority: $this->getPriority()
)
);
}
}

abstract protected function filterAndQueryItems(int $limit): Result;

/**
* @return class-string
*/
abstract protected function getModelClass(): string;

abstract protected function getChangeFrequency(): string;

abstract protected function getPriority(): float;
}
48 changes: 19 additions & 29 deletions src/ChangeFilter/ContentChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,17 @@

namespace FreshAdvance\Sitemap\ChangeFilter;

use DateTime;
use Doctrine\DBAL\Connection;
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\Eshop\Application\Model\Content;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;

class ContentChangeFilter implements ChangeFilterInterface
class ContentChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
protected Connection $connection;

public function __construct(
ConnectionProviderInterface $connectionProvider
) {
$this->connection = $connectionProvider->get();
}

public function getObjectType(): string
{
return 'content';
}

public function getUpdatedUrls(int $limit): \Generator
protected function filterAndQueryItems(int $limit): Result
{
$query = "SELECT c.OXID
FROM oxcontents c
Expand All @@ -44,6 +32,7 @@ public function getUpdatedUrls(int $limit): \Generator
ORDER BY c.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
Expand All @@ -53,20 +42,21 @@ public function getUpdatedUrls(int $limit): \Generator
]
);

while ($data = $result->fetchAssociative()) {
$item = oxNew(Content::class);
$item->load((string)$data['OXID']); // @phpstan-ignore-line
return $result;
}

yield new ObjectUrl(
objectId: $item->getId(),
objectType: $this->getObjectType(),
url: new PageUrl(
location: (string)$item->getLink(),
lastModified: new DateTime($item->getFieldData('oxtimestamp')), // @phpstan-ignore-line
changeFrequency: 'never',
priority: 0.5
)
);
}
protected function getModelClass(): string
{
return Content::class;
}

protected function getChangeFrequency(): string
{
return 'weekly';
}

protected function getPriority(): float
{
return 0.3;
}
}
48 changes: 19 additions & 29 deletions src/ChangeFilter/ProductChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,17 @@

namespace FreshAdvance\Sitemap\ChangeFilter;

use DateTime;
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\Eshop\Application\Model\Article;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
use Doctrine\DBAL\Connection;

class ProductChangeFilter implements ChangeFilterInterface
class ProductChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
protected Connection $connection;

public function __construct(
ConnectionProviderInterface $connectionProvider
) {
$this->connection = $connectionProvider->get();
}

public function getObjectType(): string
{
return 'product';
}

public function getUpdatedUrls(int $limit): iterable
protected function filterAndQueryItems(int $limit): Result
{
$query = "SELECT a.OXID
FROM oxarticles a
Expand All @@ -43,6 +31,7 @@ public function getUpdatedUrls(int $limit): iterable
ORDER BY a.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
Expand All @@ -51,20 +40,21 @@ public function getUpdatedUrls(int $limit): iterable
]
);

while ($data = $result->fetchAssociative()) {
$item = oxNew(Article::class);
$item->load((string)$data['OXID']); // @phpstan-ignore-line
return $result;
}

yield new ObjectUrl(
objectId: $item->getId(),
objectType: $this->getObjectType(),
url: new PageUrl(
location: (string)$item->getLink(),
lastModified: new DateTime($item->getFieldData('oxtimestamp')), // @phpstan-ignore-line
changeFrequency: 'daily',
priority: 0.7
)
);
}
protected function getModelClass(): string
{
return Article::class;
}

protected function getChangeFrequency(): string
{
return 'daily';
}

protected function getPriority(): float
{
return 0.5;
}
}
1 change: 0 additions & 1 deletion src/Settings/ShopSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace FreshAdvance\Sitemap\Settings;

use FreshAdvance\Sitemap\Settings\ShopSettingsInterface;
use OxidEsales\Eshop\Core\Config;

class ShopSettings implements ShopSettingsInterface
Expand Down
6 changes: 5 additions & 1 deletion tests/Integration/ChangeFilter/CategoryChangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use FreshAdvance\Sitemap\Tests\Integration\IntegrationTestCase;
use OxidEsales\Eshop\Application\Model\Category;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\CategoryChangeFilter
*/
class CategoryChangeFilterTest extends IntegrationTestCase
{
public function testGetUpdatedUrls(): void
Expand Down Expand Up @@ -62,6 +66,6 @@ protected function checkCurrentUrlItem(ObjectUrlInterface $objectUrl, string $va
$this->assertSame('http://localhost.local/' . $value . '/', $url->getLocation());
$this->assertNotEmpty($url->getLastModified());
$this->assertSame('daily', $url->getChangeFrequency());
$this->assertSame(0.9, $url->getPriority());
$this->assertSame(0.7, $url->getPriority());
}
}
5 changes: 3 additions & 2 deletions tests/Integration/ChangeFilter/ContentChangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OxidEsales\EshopCommunity\Application\Model\Content;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\ContentChangeFilter
*/
class ContentChangeFilterTest extends IntegrationTestCase
Expand Down Expand Up @@ -68,7 +69,7 @@ protected function checkCurrentUrlItem(ObjectUrlInterface $objectUrl, string $va
$url = $objectUrl->getUrl();
$this->assertSame('http://localhost.local/' . $value . '/', $url->getLocation());
$this->assertNotEmpty($url->getLastModified());
$this->assertSame('never', $url->getChangeFrequency());
$this->assertSame(0.5, $url->getPriority());
$this->assertSame('weekly', $url->getChangeFrequency());
$this->assertSame(0.3, $url->getPriority());
}
}
6 changes: 5 additions & 1 deletion tests/Integration/ChangeFilter/ProductChangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use FreshAdvance\Sitemap\DataStructure\ObjectUrlInterface;
use OxidEsales\Eshop\Application\Model\Article;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\ProductChangeFilter
*/
class ProductChangeFilterTest extends \FreshAdvance\Sitemap\Tests\Integration\IntegrationTestCase
{
public function testSomething()
Expand Down Expand Up @@ -60,6 +64,6 @@ protected function checkCurrentUrlItem(ObjectUrlInterface $objectUrl, string $va
$this->assertSame('http://localhost.local/' . $value . '.html', $url->getLocation());
$this->assertNotEmpty($url->getLastModified());
$this->assertSame('daily', $url->getChangeFrequency());
$this->assertSame(0.7, $url->getPriority());
$this->assertSame(0.5, $url->getPriority());
}
}

0 comments on commit 2adbe4c

Please sign in to comment.