Skip to content

Commit

Permalink
Extract duplication from model related 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 17, 2023
1 parent e6f3fa7 commit df84eb9
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 154 deletions.
28 changes: 4 additions & 24 deletions src/ChangeFilter/CategoryChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,17 @@

namespace FreshAdvance\Sitemap\ChangeFilter;

use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\Eshop\Application\Model\Category;

class CategoryChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
protected function getModelClass(): string
public function getUpdatedUrls(int $limit): iterable
{
return Category::class;
return $this->queryAndFetchObjectUrl($this->getQuery('oxcategories', $limit), $this->getQueryParameters());
}

protected function filterAndQueryItems(int $limit): Result
protected function getModelClass(): string
{
$query = "SELECT c.OXID
FROM oxcategories c
WHERE c.OXACTIVE = :oxactive
AND c.OXTIMESTAMP > COALESCE(
(SELECT MAX(modified) FROM fa_sitemap WHERE object_type = :object_type),
'1970-01-01'
)
ORDER BY c.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
'object_type' => $this->getObjectType(),
'oxactive' => true,
]
);

return $result;
return Category::class;
}
}
4 changes: 1 addition & 3 deletions src/ChangeFilter/ChangeFilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface ChangeFilterInterface
{
public function getObjectType(): string;

/**
* @return iterable<ObjectUrlInterface>
*/
/** @return iterable<ObjectUrlInterface> */
public function getUpdatedUrls(int $limit): iterable;
}
61 changes: 35 additions & 26 deletions src/ChangeFilter/ChangeFilterTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use FreshAdvance\Sitemap\DataStructure\ObjectUrl;
use FreshAdvance\Sitemap\DataStructure\PageUrl;
use FreshAdvance\Sitemap\PageType\PageTypeConfigurationInterface;
use FreshAdvance\Sitemap\Repository\ModelItemRepositoryInterface;
use OxidEsales\EshopCommunity\Core\Contract\IUrl;
use OxidEsales\EshopCommunity\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
Expand All @@ -25,54 +26,62 @@ abstract class ChangeFilterTemplate

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

public function getObjectType(): string
public function queryAndFetchObjectUrl(string $query, array $queryParameters): \Generator
{
return $this->pageTypeConfiguration->getObjectType();
}

public function getUpdatedUrls(int $limit): iterable
{
$result = $this->filterAndQueryItems($limit);
/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
$queryParameters
);

while ($data = $result->fetchAssociative()) {
/** @var array{OXID:string} $data */

/** @var IUrl&BaseModel $item */
$item = oxNew($this->getModelClass());
$item->load($data['OXID']);
$item = $this->modelItemRepository->getItem($data['OXID']);

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

abstract protected function filterAndQueryItems(int $limit): Result;
public function getObjectType(): string
{
return $this->pageTypeConfiguration->getObjectType();
}

/**
* @return class-string
*/
abstract protected function getModelClass(): string;
protected function getQueryParameters(): array
{
return [
'object_type' => $this->getObjectType(),
'oxactive' => true,
];
}

protected function getChangeFrequency(): string
protected function getQuery(string $table, int $limit): string
{
return $this->pageTypeConfiguration->getChangeFrequency();
$query = "SELECT c.OXID
FROM {$table} c
WHERE " . $this->getQueryCondition() . " AND c.OXTIMESTAMP > COALESCE(
(SELECT MAX(modified) FROM fa_sitemap WHERE object_type = :object_type),
'1970-01-01'
)
ORDER BY c.OXTIMESTAMP ASC
LIMIT {$limit}";
return $query;
}

protected function getPriority(): float
protected function getQueryCondition(): string
{
return $this->pageTypeConfiguration->getPriority();
return "c.OXACTIVE = :oxactive";
}
}
41 changes: 17 additions & 24 deletions src/ChangeFilter/ContentChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,32 @@

namespace FreshAdvance\Sitemap\ChangeFilter;

use Doctrine\DBAL\ForwardCompatibility\Result;
use OxidEsales\Eshop\Application\Model\Content;

class ContentChangeFilter extends ChangeFilterTemplate implements ChangeFilterInterface
{
public function getUpdatedUrls(int $limit): iterable
{
return $this->queryAndFetchObjectUrl($this->getQuery('oxcontents', $limit), $this->getQueryParameters());
}

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

protected function filterAndQueryItems(int $limit): Result
protected function getQueryParameters(): array
{
$queryParemeters = parent::getQueryParameters();

$queryParemeters['oxfolder'] = 'CMSFOLDER_USERINFO';
$queryParemeters['oxactive'] = true;

return $queryParemeters;
}

protected function getQueryCondition(): string
{
$query = "SELECT c.OXID
FROM oxcontents c
WHERE c.OXFOLDER = :oxfolder
AND c.OXACTIVE = :oxactive
AND c.OXTIMESTAMP > COALESCE(
(SELECT MAX(modified) FROM fa_sitemap WHERE object_type = :object_type),
'1970-01-01'
)
ORDER BY c.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
'object_type' => $this->getObjectType(),
'oxfolder' => 'CMSFOLDER_USERINFO',
'oxactive' => true,
]
);

return $result;
return "c.OXFOLDER = :oxfolder AND c.OXACTIVE = :oxactive";
}
}
23 changes: 2 additions & 21 deletions src/ChangeFilter/ProductChangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,8 @@ protected function getModelClass(): string
return Article::class;
}

protected function filterAndQueryItems(int $limit): Result
public function getUpdatedUrls(int $limit): iterable
{
$query = "SELECT a.OXID
FROM oxarticles a
WHERE a.OXACTIVE = :oxactive
AND a.OXTIMESTAMP > COALESCE(
(SELECT MAX(modified) FROM fa_sitemap WHERE object_type = :object_type),
'1970-01-01'
)
ORDER BY a.OXTIMESTAMP ASC
LIMIT {$limit}";

/** @var Result $result */
$result = $this->connection->executeQuery(
$query,
[
'object_type' => $this->getObjectType(),
'oxactive' => true,
]
);

return $result;
return $this->queryAndFetchObjectUrl($this->getQuery('oxarticles', $limit), $this->getQueryParameters());
}
}
3 changes: 3 additions & 0 deletions src/ChangeFilter/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ services:
FreshAdvance\Sitemap\ChangeFilter\ContentChangeFilter:
arguments:
$pageTypeConfiguration: '@FreshAdvance\Sitemap\PageType\ContentConfiguration'
$modelItemRepository: '@FreshAdvance\Sitemap\Repository\ContentModelItemRepository'
tags:
- 'sitemap.change_filter'

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

FreshAdvance\Sitemap\ChangeFilter\CategoryChangeFilter:
arguments:
$pageTypeConfiguration: '@FreshAdvance\Sitemap\PageType\CategoryConfiguration'
$modelItemRepository: '@FreshAdvance\Sitemap\Repository\CategoryModelItemRepository'
tags:
- 'sitemap.change_filter'
12 changes: 9 additions & 3 deletions src/DataStructure/ObjectUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ObjectUrl implements ObjectUrlInterface
public function __construct(
private string $objectId,
private string $objectType,
private PageUrlInterface $url
private string $location,
private \DateTimeInterface $modified,
) {
}

Expand All @@ -28,8 +29,13 @@ public function getObjectType(): string
return $this->objectType;
}

public function getUrl(): PageUrlInterface
public function getLocation(): string
{
return $this->url;
return $this->location;
}

public function getModified(): \DateTimeInterface
{
return $this->modified;
}
}
4 changes: 3 additions & 1 deletion src/DataStructure/ObjectUrlInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public function getObjectId(): string;

public function getObjectType(): string;

public function getUrl(): PageUrlInterface;
public function getLocation(): string;

public function getModified(): \DateTimeInterface;
}
14 changes: 14 additions & 0 deletions src/Exception/ModelItemNotFoundException.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 ModelItemNotFoundException extends \Exception
{
}
32 changes: 32 additions & 0 deletions src/Repository/ModelItemRepository.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 FreshAdvance\Sitemap\Repository;

use FreshAdvance\Sitemap\Exception\ModelItemNotFoundException;
use OxidEsales\Eshop\Core\Contract\IUrl;
use OxidEsales\Eshop\Core\Model\BaseModel;

class ModelItemRepository implements ModelItemRepositoryInterface
{
public function __construct(
protected string $model
) {
}

public function getItem(string $identifier): BaseModel
{
$item = oxNew($this->model);
if (!$item->load($identifier)) {
throw new ModelItemNotFoundException();
}

return $item;
}
}
17 changes: 17 additions & 0 deletions src/Repository/ModelItemRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

declare(strict_types=1);

namespace FreshAdvance\Sitemap\Repository;

use OxidEsales\Eshop\Core\Model\BaseModel;

interface ModelItemRepositoryInterface
{
public function getItem(string $identifier): BaseModel;
}
15 changes: 4 additions & 11 deletions src/Repository/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,16 @@ public function addObjectUrl(ObjectUrlInterface $objectUrl): void
object_id = :object_id,
object_type = :object_type,
location = :location,
modified = :modified,
frequency = :frequency,
priority = :priority
modified = :modified
ON DUPLICATE KEY UPDATE
location = :location,
modified = :modified,
frequency = :frequency,
priority = :priority";
modified = :modified";

$url = $objectUrl->getUrl();
$connection->executeQuery($sql, [
'object_id' => $objectUrl->getObjectId(),
'object_type' => $objectUrl->getObjectType(),
'location' => $url->getLocation(),
'modified' => $url->getLastModified()->format(\DateTimeInterface::ATOM),
'frequency' => $url->getChangeFrequency(),
'priority' => $url->getPriority()
'location' => $objectUrl->getLocation(),
'modified' => $objectUrl->getModified()->format(\DateTimeInterface::ATOM)
]);
}

Expand Down
Loading

0 comments on commit df84eb9

Please sign in to comment.