Skip to content

Commit

Permalink
Add outdated urls cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Feb 17, 2024
1 parent f173984 commit bf5196e
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [v0.1.1] - Unreleased
## [v0.2.0] - Unreleased

### Added
- Add outdated urls cleanup functionality

### Fixed
- Use correct module id during module activation migrations

## [v0.1.0] - 2024-02-10

[v0.1.1]: https://github.com/Fresh-Advance/Sitemap/compare/v0.1.0...b-7.0.x
[v0.2.0]: https://github.com/Fresh-Advance/Sitemap/compare/v0.1.0...b-7.0.x
[v0.1.0]: https://github.com/Fresh-Advance/Sitemap/compare/03839403...v0.1.0
3 changes: 3 additions & 0 deletions src/Integration/Command/UpdateTypeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$updateCount = $this->synchronizer->updateUrlsByFilter($filter);
$output->writeln("Updated items: " . $updateCount);

$cleanupCount = $this->synchronizer->cleanupUrlsByFilter($filter);
$output->writeln("Cleaned up items: " . $cleanupCount);

return Command::SUCCESS;
}
}
8 changes: 8 additions & 0 deletions src/Integration/Service/Synchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public function updateUrlsByFilter(ChangeFilterInterface $changeFilter): int

return $count;
}

public function cleanupUrlsByFilter(ChangeFilterInterface $changeFilter): int
{
$ids = $changeFilter->getDisabledUrlIds();
$this->urlRepository->deleteByIds($ids);

return count($ids);
}
}
2 changes: 2 additions & 0 deletions src/Integration/Service/SynchronizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
interface SynchronizerInterface
{
public function updateUrlsByFilter(ChangeFilterInterface $changeFilter): int;

public function cleanupUrlsByFilter(ChangeFilterInterface $changeFilter): int;
}
11 changes: 11 additions & 0 deletions src/Url/Repository/UrlRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use FreshAdvance\Sitemap\Integration\DataType\ObjectUrlInterface;
use FreshAdvance\Sitemap\Url\DataType\UrlInterface;
Expand Down Expand Up @@ -107,4 +108,14 @@ public function getUrlsCount(): int

return (int)$value;
}

public function deleteByIds(array $ids): void
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->delete('fa_sitemap')
->where("id in (:ids)")
->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY);

$queryBuilder->execute();
}
}
2 changes: 2 additions & 0 deletions src/Url/Repository/UrlRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public function getUrl(string $objectId, string $objectType): ?UrlInterface;
public function getUrls(int $page, int $perPage): iterable;

public function getUrlsCount(): int;

public function deleteByIds(array $ids): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testUpdateTypeUrls(): void
$filterStub = $this->createStub(ChangeFilterInterface::class);
$filterFactoryMock->method('getFilter')->with($exampleType)->willReturn($filterStub);
$dataSynchronizerMock->expects($this->once())->method('updateUrlsByFilter')->with($filterStub);
$dataSynchronizerMock->expects($this->once())->method('cleanupUrlsByFilter')->with($filterStub);

$commandTester = new CommandTester($command);
$commandTester->execute(['type' => $exampleType]);
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Url/Repository/UrlRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ public function testGetUrlsByTypeAndUrlsCounter(): void
$this->assertSame(10, $sut->getUrlsCount());
}

public function testDeleteByIds(): void
{
$connection = $this->getConnection();
$connection->executeQuery("delete from fa_sitemap");
$connection->executeQuery(
"insert into fa_sitemap (id, object_id, location, object_type) values
(998, 'firstobject', 'somelocation1', '{$this->objectType}'),
(999, 'secondobject', 'somelocation2', '{$this->objectType}'),
(1000, 'thirdobject', 'somelocation3', '{$this->objectType}'),
(1001, 'fourthobject', 'somelocation4', 'not content')"
);

$ids = [999, 1000];

$sut = $this->getSut();
$sut->deleteByIds($ids);

$idsLeft = $connection->executeQuery("select id from fa_sitemap")->fetchFirstColumn();
$this->assertSame([998, 1001], $idsLeft);
}

protected function getSut(): UrlRepository
{
return $this->get(UrlRepositoryInterface::class);
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Integration/Service/SynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use FreshAdvance\Sitemap\Integration\Contract\ChangeFilterInterface;
use FreshAdvance\Sitemap\Integration\DataType\ObjectUrlInterface;
use FreshAdvance\Sitemap\Integration\Service\FilterFactoryInterface;
use FreshAdvance\Sitemap\Integration\Service\Synchronizer;
use FreshAdvance\Sitemap\Url\Repository\UrlRepositoryInterface;
use Generator;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -63,6 +64,21 @@ function (
$this->assertSame(2, $sut->updateUrlsByFilter($filterStub));
}

public function testCleanupTriggersUrlRepositoryCleanupsWithFilteredIds(): void
{
$ids = [3, 5];

$filterStub = $this->createMock(ChangeFilterInterface::class);
$filterStub->method('getDisabledUrlIds')->willReturn($ids);

$sut = new Synchronizer(
urlRepository: $urlRepositorySpy = $this->createMock(UrlRepositoryInterface::class)
);
$urlRepositorySpy->expects($this->once())->method('deleteByIds')->with($ids);

$this->assertSame(2, $sut->cleanupUrlsByFilter($filterStub));
}

protected function arrayAsGenerator(array $array): Generator
{
foreach ($array as $key => $item) {
Expand Down

0 comments on commit bf5196e

Please sign in to comment.