From 500ca7b31ea94f8813fbf81d659d4e6a68c2669a Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 4 Mar 2021 11:58:45 +0100 Subject: [PATCH] Refactor RedirectTrackingHandler to remove duplicate code --- .../Routing/RedirectTrackingHandler.cs | 38 ++++++------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/Umbraco.Infrastructure/Routing/RedirectTrackingHandler.cs b/src/Umbraco.Infrastructure/Routing/RedirectTrackingHandler.cs index bacbbc91b9..75a554867d 100644 --- a/src/Umbraco.Infrastructure/Routing/RedirectTrackingHandler.cs +++ b/src/Umbraco.Infrastructure/Routing/RedirectTrackingHandler.cs @@ -47,44 +47,28 @@ namespace Umbraco.Cms.Core.Routing _requestCache = requestCache; } - public void Handle(PublishingNotification notification) + public void Handle(PublishingNotification notification) => StoreOldRoutes(notification.PublishedEntities); + + public void Handle(PublishedNotification notification) => CreateRedirectsForOldRoutes(); + + public void Handle(MovingNotification notification) => StoreOldRoutes(notification.MoveInfoCollection.Select(m => m.Entity)); + + public void Handle(MovedNotification notification) => CreateRedirectsForOldRoutes(); + + private void StoreOldRoutes(IEnumerable entities) { // don't let the notification handlers kick in if Redirect Tracking is turned off in the config if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking) return; var oldRoutes = GetOldRoutes(); - foreach (var entity in notification.PublishedEntities) + foreach (var entity in entities) { StoreOldRoute(entity, oldRoutes); } } - public void Handle(PublishedNotification notification) - { - // don't let the notification handlers kick in if Redirect Tracking is turned off in the config - if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking) - return; - - var oldRoutes = GetOldRoutes(); - CreateRedirects(oldRoutes); - } - - public void Handle(MovingNotification notification) - { - // don't let the notification handlers kick in if Redirect Tracking is turned off in the config - if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking) - return; - - var oldRoutes = GetOldRoutes(); - foreach (var info in notification.MoveInfoCollection) - { - StoreOldRoute(info.Entity, oldRoutes); - } - } - - // TODO refactor (this is duplicate code, see published notification handling above) - public void Handle(MovedNotification notification) + private void CreateRedirectsForOldRoutes() { // don't let the notification handlers kick in if Redirect Tracking is turned off in the config if (_webRoutingSettings.CurrentValue.DisableRedirectUrlTracking)