77use App \Entity \DetectionResult ;
88use App \Entity \Installation ;
99use App \Entity \Site ;
10+ use App \Repository \DetectionResultRepository ;
1011use App \Repository \SiteRepository ;
1112use App \Service \InstallationFactory ;
1213use 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).
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}
0 commit comments