Skip to content

Commit 80ba29f

Browse files
committed
3666: Cleanup orphaned detection results when installations are removed from server
1 parent 229a94f commit 80ba29f

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/Handler/DirectoryHandler.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
use App\Entity\DetectionResult;
88
use App\Entity\Installation;
99
use App\Entity\Site;
10+
use App\Repository\DetectionResultRepository;
1011
use App\Repository\SiteRepository;
1112
use App\Service\InstallationFactory;
1213
use App\Types\DetectionType;
14+
use Doctrine\Common\Collections\Collection;
15+
use Doctrine\ORM\EntityManager;
16+
use Doctrine\ORM\EntityManagerInterface;
1317

1418
/**
1519
* Handler for DetectionResult off type "dir" (Installations).
@@ -25,6 +29,7 @@
2529
public function __construct(
2630
private SiteRepository $siteRepository,
2731
private InstallationFactory $installationFactory,
32+
private DetectionResultRepository $detectionResultRepository,
2833
) {
2934
}
3035

@@ -33,13 +38,16 @@ public function __construct(
3338
*/
3439
public function handleResult(DetectionResult $detectionResult): void
3540
{
41+
$server = $detectionResult->getServer();
42+
$oldInstallations = $server->getInstallations();
43+
3644
$installations = $this->installationFactory->getInstallations($detectionResult);
3745

3846
/** @var Installation $installation */
3947
foreach ($installations as $installation) {
4048
$sites = $this->siteRepository->findByRootDirAndServer(
4149
$installation->getRootDir(),
42-
$detectionResult->getServer()
50+
$server
4351
);
4452
/* @var Site $site */
4553
foreach ($sites as $site) {
@@ -48,6 +56,14 @@ public function handleResult(DetectionResult $detectionResult): void
4856
}
4957

5058
$detectionResult->getServer()->setInstallations($installations);
59+
60+
// Delete results from installations no longer seen on the server.
61+
foreach ($oldInstallations as $oldInstallation) {
62+
if (!$installations->contains($oldInstallation)) {
63+
$this->detectionResultRepository->deleteByInstallation($oldInstallation);
64+
}
65+
}
66+
5167
}
5268

5369
/**
@@ -57,4 +73,16 @@ public function supportsType(string $type): bool
5773
{
5874
return DetectionType::DIRECTORY === $type;
5975
}
76+
77+
private function getRemovedInstallations(Collection $oldInstallations, Collection $newInstallations): array
78+
{
79+
$result = [];
80+
foreach ($oldInstallations as $installation) {
81+
if (!$newInstallations->contains($installation)) {
82+
$result[] = $installation;
83+
}
84+
}
85+
86+
return $result;
87+
}
6088
}

src/Repository/DetectionResultRepository.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace App\Repository;
66

77
use App\Entity\DetectionResult;
8+
use App\Entity\Installation;
9+
use App\Entity\Server;
810
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
911
use Doctrine\Persistence\ManagerRegistry;
1012

@@ -40,6 +42,15 @@ public function remove(\DateTime $date): mixed
4042
->getResult();
4143
}
4244

45+
/**
46+
* Delete all except X similar detection results
47+
*
48+
* @param DetectionResult $detectionResult
49+
* @param int $keep
50+
* @param bool $flush
51+
*
52+
* @return void
53+
*/
4354
public function cleanup(DetectionResult $detectionResult, int $keep = 5, bool $flush = false): void
4455
{
4556
if ($keep < 1) {
@@ -65,4 +76,22 @@ public function cleanup(DetectionResult $detectionResult, int $keep = 5, bool $f
6576
$em->flush();
6677
}
6778
}
79+
80+
/**
81+
* Delete all detection results for a given installation.
82+
*
83+
* @param Installation $installation
84+
*
85+
* @return void
86+
*/
87+
public function deleteByInstallation(Installation $installation): void
88+
{
89+
$qb = $this->getEntityManager()->createQueryBuilder();
90+
$qb->delete(DetectionResult::class, 'd')
91+
->where('d.server = :server')
92+
->andWhere('d.rootDir =:rootDir')
93+
->setParameter('server', $installation->getServer())
94+
->setParameter('rootDir', $installation->getRootDir());
95+
$qb->getQuery()->execute();
96+
}
6897
}

0 commit comments

Comments
 (0)