Skip to content

Commit

Permalink
Add General urls change filter with shop main url
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Jan 3, 2024
1 parent abe5ca3 commit 3c1bd0c
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/ChangeFilter/Category/CategoryChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace FreshAdvance\Sitemap\ChangeFilter\Category;

use FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate;
use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface;
use FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter;

class CategoryChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
class CategoryChangeFilter extends DatabaseChangeFilter
{
public function getUpdatedUrls(int $limit): iterable
{
Expand Down
5 changes: 2 additions & 3 deletions src/ChangeFilter/Content/ContentChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace FreshAdvance\Sitemap\ChangeFilter\Content;

use FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate;
use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface;
use FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter;

class ContentChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
class ContentChangeFilter extends DatabaseChangeFilter
{
public function getUpdatedUrls(int $limit): iterable
{
Expand Down
42 changes: 42 additions & 0 deletions src/ChangeFilter/General/GeneralChangeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\ChangeFilter\General;

use FreshAdvance\Sitemap\ChangeFilter\Shared\BaseChangeFilter;
use FreshAdvance\Sitemap\Integration\DataType\ObjectUrl;
use FreshAdvance\Sitemap\Settings\ShopSettingsInterface;

class GeneralChangeFilter extends BaseChangeFilter
{
public function __construct(
string $objectType,
protected ShopSettingsInterface $shopSettings,
) {
parent::__construct($objectType);
}

public function getUpdatedUrls(int $limit): \Generator
{
yield new ObjectUrl(
objectId: "main",
objectType: $this->getObjectType(),
location: $this->prepareUrl('/'),
modified: new \DateTime()
);
}

protected function prepareUrl(string $url): string
{
return implode("/", [
rtrim($this->shopSettings->getShopUrl()),
$url
]);
}
}
23 changes: 23 additions & 0 deletions src/ChangeFilter/General/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
parameters:
sitemap.object_type.general: 'general'

services:

_defaults:
public: false
autowire: true

FreshAdvance\Sitemap\ChangeFilter\General\GeneralChangeFilter:
arguments:
$objectType: '%sitemap.object_type.general%'
tags:
- 'sitemap.change_filter'

FreshAdvance\Sitemap\ChangeFilter\General\GeneralUrlType:
class: FreshAdvance\Sitemap\Url\DataType\UrlType
arguments:
$objectType: '%sitemap.object_type.general%'
$changeFrequency: 'daily'
$priority: 0.85
tags:
- 'sitemap.page_type_configuration'
5 changes: 2 additions & 3 deletions src/ChangeFilter/Product/ProductChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace FreshAdvance\Sitemap\ChangeFilter\Product;

use FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate;
use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface;
use FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter;

class ProductChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
class ProductChangeFilter extends DatabaseChangeFilter
{
public function getUpdatedUrls(int $limit): iterable
{
Expand Down
27 changes: 27 additions & 0 deletions src/ChangeFilter/Shared/BaseChangeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\ChangeFilter\Shared;

use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface;

abstract class BaseChangeFilter implements ChangeFilterInterface
{
public function __construct(
protected string $objectType
) {
}

public function getObjectType(): string
{
return $this->objectType;
}

abstract public function getUpdatedUrls(int $limit): iterable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
use Doctrine\DBAL\ForwardCompatibility\Result;
use FreshAdvance\Sitemap\ChangeFilter\Shared\Repository\ModelItemRepositoryInterface;
use FreshAdvance\Sitemap\Integration\DataType\ObjectUrl;
use FreshAdvance\Sitemap\Url\DataType\UrlTypeInterface;
use Generator;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;

abstract class ChangeFilterTemplate
abstract class DatabaseChangeFilter extends BaseChangeFilter
{
protected Connection $connection;

Expand All @@ -27,6 +26,10 @@ public function __construct(
protected string $objectType,
protected ModelItemRepositoryInterface $modelItemRepository,
) {
parent::__construct(
objectType: $this->objectType
);

$this->connection = $connectionProvider->get();
}

Expand Down Expand Up @@ -55,11 +58,6 @@ public function queryAndFetchObjectUrl(string $query, array $queryParameters): G
}
}

public function getObjectType(): string
{
return $this->objectType;
}

protected function getQueryParameters(): array
{
return [
Expand Down
1 change: 1 addition & 0 deletions src/ChangeFilter/services.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
imports:
- { resource: Category/services.yaml }
- { resource: Content/services.yaml }
- { resource: General/services.yaml }
- { resource: Product/services.yaml }
2 changes: 1 addition & 1 deletion src/Integration/Service/FilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
) {
foreach ($filters as $oneFilter) {
if (!$oneFilter instanceof ChangeFilterInterface) {
throw new FilterConfigurationException();
throw new FilterConfigurationException($oneFilter::class);
}
$this->filters[$oneFilter->getObjectType()] = $oneFilter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use OxidEsales\Eshop\Application\Model\Category;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter
* @covers \FreshAdvance\Sitemap\ChangeFilter\Category\CategoryChangeFilter
*/
class CategoryChangeFilterTest extends IntegrationTestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use OxidEsales\EshopCommunity\Application\Model\Content;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter
* @covers \FreshAdvance\Sitemap\ChangeFilter\Content\ContentChangeFilter
*/
class ContentChangeFilterTest extends IntegrationTestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use OxidEsales\Eshop\Application\Model\Article;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\ChangeFilterTemplate
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\DatabaseChangeFilter
* @covers \FreshAdvance\Sitemap\ChangeFilter\Product\ProductChangeFilter
*/
class ProductChangeFilterTest extends IntegrationTestCase
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Service/FilterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\Tests\Integration\Service;

use FreshAdvance\Sitemap\Integration\Service\FilterFactoryInterface;
use FreshAdvance\Sitemap\Tests\Integration\IntegrationTestCase;

/**
* @covers \FreshAdvance\Sitemap\Integration\Service\FilterFactory
*/
class FilterFactoryTest extends IntegrationTestCase
{
public function testInitialization(): void
{
$sut = $this->get(FilterFactoryInterface::class);
$this->assertInstanceOf(FilterFactoryInterface::class, $sut);
}
}
25 changes: 25 additions & 0 deletions tests/Integration/Url/DataTypeFactory/UrlTypeFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\Tests\Integration\Url\DataTypeFactory;

use FreshAdvance\Sitemap\Tests\Integration\IntegrationTestCase;
use FreshAdvance\Sitemap\Url\DataTypeFactory\UrlTypeFactoryInterface;

/**
* @covers \FreshAdvance\Sitemap\Url\DataTypeFactory\UrlTypeFactory
*/
class UrlTypeFactoryTest extends IntegrationTestCase
{
public function testInitialization(): void
{
$sut = $this->get(UrlTypeFactoryInterface::class);
$this->assertInstanceOf(UrlTypeFactoryInterface::class, $sut);
}
}
38 changes: 38 additions & 0 deletions tests/Unit/ChangeFilter/General/GeneralChangeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

declare(strict_types=1);

namespace ChangeFilter\General;

use FreshAdvance\Sitemap\ChangeFilter\General\GeneralChangeFilter;
use FreshAdvance\Sitemap\Integration\DataType\ObjectUrl;
use FreshAdvance\Sitemap\Settings\ShopSettingsInterface;
use PHPUnit\Framework\TestCase;

class GeneralChangeFilterTest extends TestCase
{
public function testGetUpdatedUrls(): void
{
$sut = new GeneralChangeFilter(
objectType: "someObjectType",
shopSettings: $shopSettingsStub = $this->createStub(ShopSettingsInterface::class)
);

$shopUrlExample = "shopUrlStart";
$shopSettingsStub->method('getShopUrl')->willReturn($shopUrlExample);

$count = 0;
foreach ($sut->getUpdatedUrls(1000) as $oneObjectUrl) {
$this->assertInstanceOf(ObjectUrl::class, $oneObjectUrl);
$this->assertStringStartsWith($shopUrlExample, $oneObjectUrl->getLocation());
$count++;
}

$this->assertTrue($count > 0);
}
}
32 changes: 32 additions & 0 deletions tests/Unit/ChangeFilter/Shared/BaseChangeFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

declare(strict_types=1);

namespace ChangeFilter\Shared;

use FreshAdvance\Sitemap\ChangeFilter\Shared\BaseChangeFilter;
use PHPUnit\Framework\TestCase;

/**
* @covers \FreshAdvance\Sitemap\ChangeFilter\Shared\BaseChangeFilter
*/
class BaseChangeFilterTest extends TestCase
{
public function testGetObjectType(): void
{
$objectType = uniqid();

$sut = new class($objectType) extends BaseChangeFilter {
public function getUpdatedUrls(int $limit): iterable
{
}
};

$this->assertSame($objectType, $sut->getObjectType());
}
}

0 comments on commit 3c1bd0c

Please sign in to comment.