Skip to content

Commit

Permalink
Fixed circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Migaroez committed Oct 23, 2024
1 parent 098bb79 commit 89eca37
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NPoco;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
Expand All @@ -23,7 +22,6 @@ public class ConvertLocalLinks : MigrationBase
private readonly ILanguageService _languageService;
private readonly IJsonSerializer _jsonSerializer;
private readonly LocalLinkProcessor _convertLocalLinkProcessor;
private readonly IOptions<ConvertLocalLinkOptions> _options;

public ConvertLocalLinks(
IMigrationContext context,
Expand All @@ -33,8 +31,7 @@ public ConvertLocalLinks(
IDataTypeService dataTypeService,
ILanguageService languageService,
IJsonSerializer jsonSerializer,
LocalLinkProcessor convertLocalLinkProcessor,
IOptions<ConvertLocalLinkOptions> options)
LocalLinkProcessor convertLocalLinkProcessor)
: base(context)
{
_umbracoContextFactory = umbracoContextFactory;
Expand All @@ -44,12 +41,10 @@ public ConvertLocalLinks(
_languageService = languageService;
_jsonSerializer = jsonSerializer;
_convertLocalLinkProcessor = convertLocalLinkProcessor;
_options = options;
}

Check warning on line 44 in src/Umbraco.Infrastructure/Migrations/Upgrade/V_15_0_0/ConvertLocalLinks.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Constructor Over-Injection

ConvertLocalLinks has 8 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.

protected override void Migrate()
{
_convertLocalLinkProcessor.Initialize(_options.Value.Processors);
IEnumerable<string> propertyEditorAliases = _convertLocalLinkProcessor.GetSupportedPropertyEditorAliases();

using UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models.Blocks;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Templates;
using Umbraco.Extensions;
Expand All @@ -17,8 +18,8 @@ public void Compose(IUmbracoBuilder builder)
builder.Services.AddUnique<LocalLinkProcessor>(provider =>
new LocalLinkProcessor(
provider.GetRequiredService<HtmlLocalLinkParser>(),
provider.GetRequiredService<IdKeyMap>(),
provider.GetRequiredService<IOptions<ConvertLocalLinkOptions>>()));
provider.GetRequiredService<IIdKeyMap>(),
provider.GetRequiredService<Lazy<IOptions<ConvertLocalLinkOptions>>>()));
builder.Services.AddUnique<LocalLinkBlocksProcessor>(provider => new LocalLinkBlocksProcessor(provider.GetRequiredService<LocalLinkProcessor>()));
builder.Services.AddUnique<LocalLinkRteProcessor>(provider => new LocalLinkRteProcessor(provider.GetRequiredService<LocalLinkProcessor>()));
builder.Services.ConfigureOptions<ConfigureConvertLocalLinkOptions>();
Expand Down Expand Up @@ -47,8 +48,13 @@ public void Configure(ConvertLocalLinkOptions options)
_linkLinkRteProcessor.ProcessRichText));

options.Processors.Add(new ProcessorInformation(
typeof(RichTextEditorValue),
[Constants.PropertyEditors.Aliases.BlockList, Constants.PropertyEditors.Aliases.BlockGrid],
typeof(BlockListValue),
[Constants.PropertyEditors.Aliases.BlockList],
_localLinkBlocksProcessor.ProcessBlocks));

options.Processors.Add(new ProcessorInformation(
typeof(BlockGridValue),
[Constants.PropertyEditors.Aliases.BlockGrid],
_localLinkBlocksProcessor.ProcessBlocks));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_15_0_0.LocalLinks;
[Obsolete("Will be removed in V18")]
public class ConvertLocalLinkOptions
{
public List<ProcessorInformation> Processors { get; } = new();
public List<ProcessorInformation> Processors { get; } = [];
}

[Obsolete("Will be removed in V18")]
public class ProcessorInformation
{
public Type PropertyEditorValueType { get; init; }
public Type PropertyEditorValueType { get; }

public IEnumerable<string> PropertyEditorAliases { get; init; }
public IEnumerable<string> PropertyEditorAliases { get; }

public Func<object?, bool> Process { get; init; }
public Func<object?, bool> Process { get; }

public ProcessorInformation(Type propertyEditorValueType, IEnumerable<string> propertyEditorAliases, Func<object?, bool> process)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ public class LocalLinkProcessor
{
private readonly HtmlLocalLinkParser _localLinkParser;
private readonly IIdKeyMap _idKeyMap;
private List<ProcessorInformation> _processors;
private readonly Lazy<IOptions<ConvertLocalLinkOptions>> _lazyOptions;

public LocalLinkProcessor(
HtmlLocalLinkParser localLinkParser,
IIdKeyMap idKeyMap,
IOptions<ConvertLocalLinkOptions> options)
Lazy<IOptions<ConvertLocalLinkOptions>> lazyOptions)
{
_localLinkParser = localLinkParser;
_idKeyMap = idKeyMap;
_processors = options.Value.Processors;
_lazyOptions = lazyOptions;
}

public IEnumerable<string> GetSupportedPropertyEditorAliases() =>
_processors.SelectMany(p => p.PropertyEditorAliases);
_lazyOptions.Value.Value.Processors.SelectMany(p => p.PropertyEditorAliases);

public bool ProcessToEditorValue(object? editorValue)
{
ProcessorInformation? processor = _processors.FirstOrDefault(p => p.PropertyEditorValueType == editorValue?.GetType());
ProcessorInformation? processor = _lazyOptions.Value.Value.Processors.FirstOrDefault(p => p.PropertyEditorValueType == editorValue?.GetType());

return processor is not null && processor.Process.Invoke(editorValue);
}

public string ProcessStringValue(string input)
{
// find all legacy tags
IEnumerable<HtmlLocalLinkParser.LocalLinkTag> tags = _localLinkParser.FindLegacyLocalLinkIds(input);
var tags = _localLinkParser.FindLegacyLocalLinkIds(input).ToList();

foreach (HtmlLocalLinkParser.LocalLinkTag tag in tags)
{
Expand Down

0 comments on commit 89eca37

Please sign in to comment.