Skip to content

Commit

Permalink
Extract page type configurations from 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 4, 2023
1 parent c0b3dc1 commit 499fbb2
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 58 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"phpcbf": "phpcbf --standard=tests/phpcs.xml",
"phpstan": "phpstan -ctests/PhpStan/phpstan.neon analyse src/",
"phpstan-report": "phpstan -ctests/PhpStan/phpstan.neon analyse src/ --error-format=json > phpstan.report.json",
"phpmd": "phpmd src text cleancode,codesize,design,naming,unusedcode",
"phpmd-report": "phpmd src json cleancode,codesize,design,naming,unusedcode --reportfile phpmd.report.json",
"phpmd": "phpmd src text cleancode,codesize,design,unusedcode",
"phpmd-report": "phpmd src json cleancode,codesize,design,unusedcode --reportfile phpmd.report.json",
"static": [
"@phpcs",
"@phpstan",
Expand Down
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imports:
- { resource: src/ChangeFilter/services.yaml }
- { resource: src/Command/services.yaml }
- { resource: src/PageType/services.yaml }
- { resource: src/Repository/services.yaml }
- { resource: src/Service/services.yaml }
- { resource: src/Settings/services.yaml }
Expand Down
15 changes: 0 additions & 15 deletions src/ChangeFilter/CategoryChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,11 @@

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

protected function getModelClass(): string
{
return Category::class;
}

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

protected function getPriority(): float
{
return 0.7;
}

protected function filterAndQueryItems(int $limit): Result
{
$query = "SELECT c.OXID
Expand Down
19 changes: 15 additions & 4 deletions src/ChangeFilter/ChangeFilterTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\DBAL\ForwardCompatibility\Result;
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use FreshAdvance\Sitemap\PageType\PageTypeConfigurationInterface;
use OxidEsales\EshopCommunity\Core\Contract\IUrl;
use OxidEsales\EshopCommunity\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
Expand All @@ -23,12 +24,16 @@ abstract class ChangeFilterTemplate
protected Connection $connection;

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

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

public function getUpdatedUrls(int $limit): iterable
{
Expand Down Expand Up @@ -61,7 +66,13 @@ abstract protected function filterAndQueryItems(int $limit): Result;
*/
abstract protected function getModelClass(): string;

abstract protected function getChangeFrequency(): string;
protected function getChangeFrequency(): string
{
return $this->pageTypeConfiguration->getChangeFrequency();
}

abstract protected function getPriority(): float;
protected function getPriority(): float
{
return $this->pageTypeConfiguration->getPriority();
}
}
19 changes: 2 additions & 17 deletions src/ChangeFilter/ContentChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class ContentChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
public function getObjectType(): string
protected function getModelClass(): string
{
return 'content';
return Content::class;
}

protected function filterAndQueryItems(int $limit): Result
Expand Down Expand Up @@ -44,19 +44,4 @@ protected function filterAndQueryItems(int $limit): Result

return $result;
}

protected function getModelClass(): string
{
return Content::class;
}

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

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

class ProductChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
public function getObjectType(): string
protected function getModelClass(): string
{
return 'product';
return Article::class;
}

protected function filterAndQueryItems(int $limit): Result
Expand All @@ -42,19 +42,4 @@ protected function filterAndQueryItems(int $limit): Result

return $result;
}

protected function getModelClass(): string
{
return Article::class;
}

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

protected function getPriority(): float
{
return 0.5;
}
}
6 changes: 6 additions & 0 deletions src/ChangeFilter/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ services:
$filters: !tagged 'sitemap.change_filter'

FreshAdvance\Sitemap\ChangeFilter\ContentChangeFilter:
arguments:
$pageTypeConfiguration: '@FreshAdvance\Sitemap\PageType\ContentConfiguration'
tags:
- 'sitemap.change_filter'

FreshAdvance\Sitemap\ChangeFilter\ProductChangeFilter:
arguments:
$pageTypeConfiguration: '@FreshAdvance\Sitemap\PageType\ProductConfiguration'
tags:
- 'sitemap.change_filter'

FreshAdvance\Sitemap\ChangeFilter\CategoryChangeFilter:
arguments:
$pageTypeConfiguration: '@FreshAdvance\Sitemap\PageType\CategoryConfiguration'
tags:
- 'sitemap.change_filter'
4 changes: 3 additions & 1 deletion src/Command/UpdateTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace FreshAdvance\Sitemap\Command;

use FreshAdvance\Sitemap\ChangeFilter\FilterFactoryInterface;
use FreshAdvance\Sitemap\Service\SynchronizerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -18,7 +19,8 @@
class UpdateTypeCommand extends Command
{
public function __construct(
private SynchronizerInterface $synchronizer
protected SynchronizerInterface $synchronizer,
protected FilterFactoryInterface $filterFactory,
) {
parent::__construct();
}
Expand Down
14 changes: 14 additions & 0 deletions src/Exception/PageTypeConfigurationFactorySetupException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\Exception;

class PageTypeConfigurationFactorySetupException extends \Exception
{
}
35 changes: 35 additions & 0 deletions src/PageType/PageTypeConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\PageType;

class PageTypeConfiguration implements PageTypeConfigurationInterface
{
public function __construct(
protected string $objectType,
protected string $changeFrequency,
protected float $priority
) {
}

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

public function getChangeFrequency(): string
{
return $this->changeFrequency;
}

public function getPriority(): float
{
return $this->priority;
}
}
39 changes: 39 additions & 0 deletions src/PageType/PageTypeConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\PageType;

use FreshAdvance\Sitemap\Exception\PageTypeConfigurationFactorySetupException;

class PageTypeConfigurationFactory implements PageTypeConfigurationFactoryInterface
{
protected array $configurations = [];

/**
* @param iterable<PageTypeConfigurationInterface> $configurations
* @throws PageTypeConfigurationFactorySetupException
*/
public function __construct(
iterable $configurations,
protected PageTypeConfigurationInterface $defaultConfiguration
) {
foreach ($configurations as $oneConfiguration) {
if (!$oneConfiguration instanceof PageTypeConfigurationInterface) {
throw new PageTypeConfigurationFactorySetupException();
}

$this->configurations[$oneConfiguration->getObjectType()] = $oneConfiguration;
}
}

public function getConfiguration(string $string): PageTypeConfigurationInterface
{
return $this->configurations[$string] ?? $this->defaultConfiguration;
}
}
15 changes: 15 additions & 0 deletions src/PageType/PageTypeConfigurationFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\PageType;

interface PageTypeConfigurationFactoryInterface
{
public function getConfiguration(string $string): PageTypeConfigurationInterface;
}
19 changes: 19 additions & 0 deletions src/PageType/PageTypeConfigurationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\PageType;

interface PageTypeConfigurationInterface
{
public function getObjectType(): string;

public function getChangeFrequency(): string;

public function getPriority(): float;
}
45 changes: 45 additions & 0 deletions src/PageType/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
services:

_defaults:
public: false
autowire: true

FreshAdvance\Sitemap\PageType\PageTypeConfigurationFactoryInterface:
class: FreshAdvance\Sitemap\PageType\PageTypeConfigurationFactory
arguments:
$configurations: !tagged 'sitemap.page_type_configuration'
$defaultConfiguration: '@FreshAdvance\Sitemap\PageType\DefaultConfiguration'

FreshAdvance\Sitemap\PageType\DefaultConfiguration:
class: FreshAdvance\Sitemap\PageType\PageTypeConfiguration
arguments:
$objectType: 'default'
$changeFrequency: 'daily'
$priority: 0.5

FreshAdvance\Sitemap\PageType\ProductConfiguration:
class: FreshAdvance\Sitemap\PageType\PageTypeConfiguration
arguments:
$objectType: 'product'
$changeFrequency: 'daily'
$priority: 0.4
tags:
- 'sitemap.page_type_configuration'

FreshAdvance\Sitemap\PageType\CategoryConfiguration:
class: FreshAdvance\Sitemap\PageType\PageTypeConfiguration
arguments:
$objectType: 'category'
$changeFrequency: 'daily'
$priority: 0.7
tags:
- 'sitemap.page_type_configuration'

FreshAdvance\Sitemap\PageType\ContentConfiguration:
class: FreshAdvance\Sitemap\PageType\PageTypeConfiguration
arguments:
$objectType: 'content'
$changeFrequency: 'weekly'
$priority: 0.3
tags:
- 'sitemap.page_type_configuration'
2 changes: 1 addition & 1 deletion tests/Integration/ChangeFilter/ProductChangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,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.5, $url->getPriority());
$this->assertSame(0.4, $url->getPriority());
}
}
4 changes: 3 additions & 1 deletion tests/Integration/Command/UpdateTypeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Command;

use FreshAdvance\Sitemap\ChangeFilter\FilterFactoryInterface;
use FreshAdvance\Sitemap\Command\UpdateTypeCommand;
use FreshAdvance\Sitemap\Service\Synchronizer;
use Symfony\Component\Console\Tester\CommandTester;
Expand All @@ -21,7 +22,8 @@ class UpdateTypeCommandTest extends \PHPUnit\Framework\TestCase
public function testUpdateTypeUrls(): void
{
$command = new UpdateTypeCommand(
synchronizer: $dataSynchronizerMock = $this->createMock(Synchronizer::class)
synchronizer: $dataSynchronizerMock = $this->createMock(Synchronizer::class),
filterFactory: $filterFactoryMock = $this->createMock(FilterFactoryInterface::class),
);

$dataSynchronizerMock->expects($this->once())->method('updateTypeUrls')->with('someType');
Expand Down
Loading

0 comments on commit 499fbb2

Please sign in to comment.