From bf5196ecf7a48d4a915bf905d00776409172038e Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Sat, 17 Feb 2024 13:38:55 +0200 Subject: [PATCH] Add outdated urls cleanup Signed-off-by: Anton Fedurtsya --- CHANGELOG.md | 7 +++++-- src/Integration/Command/UpdateTypeCommand.php | 3 +++ src/Integration/Service/Synchronizer.php | 8 +++++++ .../Service/SynchronizerInterface.php | 2 ++ src/Url/Repository/UrlRepository.php | 11 ++++++++++ src/Url/Repository/UrlRepositoryInterface.php | 2 ++ .../Command/UpdateTypeCommandTest.php | 1 + .../Url/Repository/UrlRepositoryTest.php | 21 +++++++++++++++++++ .../Integration/Service/SynchronizerTest.php | 16 ++++++++++++++ 9 files changed, 69 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b1b3f5..a444958 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Integration/Command/UpdateTypeCommand.php b/src/Integration/Command/UpdateTypeCommand.php index 1aa9939..e35fe19 100644 --- a/src/Integration/Command/UpdateTypeCommand.php +++ b/src/Integration/Command/UpdateTypeCommand.php @@ -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; } } diff --git a/src/Integration/Service/Synchronizer.php b/src/Integration/Service/Synchronizer.php index bb52cb0..31d6dbc 100644 --- a/src/Integration/Service/Synchronizer.php +++ b/src/Integration/Service/Synchronizer.php @@ -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); + } } diff --git a/src/Integration/Service/SynchronizerInterface.php b/src/Integration/Service/SynchronizerInterface.php index 024b846..ad7a992 100644 --- a/src/Integration/Service/SynchronizerInterface.php +++ b/src/Integration/Service/SynchronizerInterface.php @@ -12,4 +12,6 @@ interface SynchronizerInterface { public function updateUrlsByFilter(ChangeFilterInterface $changeFilter): int; + + public function cleanupUrlsByFilter(ChangeFilterInterface $changeFilter): int; } diff --git a/src/Url/Repository/UrlRepository.php b/src/Url/Repository/UrlRepository.php index 3ec08ee..26cda03 100644 --- a/src/Url/Repository/UrlRepository.php +++ b/src/Url/Repository/UrlRepository.php @@ -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; @@ -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(); + } } diff --git a/src/Url/Repository/UrlRepositoryInterface.php b/src/Url/Repository/UrlRepositoryInterface.php index 4cba8a1..567659a 100644 --- a/src/Url/Repository/UrlRepositoryInterface.php +++ b/src/Url/Repository/UrlRepositoryInterface.php @@ -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; } diff --git a/tests/Integration/Integration/Command/UpdateTypeCommandTest.php b/tests/Integration/Integration/Command/UpdateTypeCommandTest.php index 5c584d9..46fa747 100644 --- a/tests/Integration/Integration/Command/UpdateTypeCommandTest.php +++ b/tests/Integration/Integration/Command/UpdateTypeCommandTest.php @@ -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]); diff --git a/tests/Integration/Url/Repository/UrlRepositoryTest.php b/tests/Integration/Url/Repository/UrlRepositoryTest.php index 5e7e969..b356fd9 100644 --- a/tests/Integration/Url/Repository/UrlRepositoryTest.php +++ b/tests/Integration/Url/Repository/UrlRepositoryTest.php @@ -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); diff --git a/tests/Unit/Integration/Service/SynchronizerTest.php b/tests/Unit/Integration/Service/SynchronizerTest.php index d6eb0f2..cc94e98 100644 --- a/tests/Unit/Integration/Service/SynchronizerTest.php +++ b/tests/Unit/Integration/Service/SynchronizerTest.php @@ -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; @@ -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) {