Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;

Check warning on line 1 in src/Umbraco.Infrastructure/PropertyEditors/MediaPicker3PropertyEditor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ New issue: Complex Method

Validate has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
Expand Down Expand Up @@ -82,7 +82,8 @@
IDataTypeConfigurationCache dataTypeReadCache,
ILocalizedTextService localizedTextService,
IMediaTypeService mediaTypeService,
IMediaNavigationQueryService mediaNavigationQueryService)
IMediaNavigationQueryService mediaNavigationQueryService,
AppCaches appCaches)
: base(shortStringHelper, jsonSerializer, ioHelper, attribute)
{
_jsonSerializer = jsonSerializer;
Expand All @@ -95,7 +96,7 @@
var validators = new TypedJsonValidatorRunner<List<MediaWithCropsDto>, MediaPicker3Configuration>(
jsonSerializer,
new MinMaxValidator(localizedTextService),
new AllowedTypeValidator(localizedTextService, mediaTypeService, _mediaService),
new AllowedTypeValidator(localizedTextService, mediaTypeService, _mediaService, appCaches),
new StartNodeValidator(localizedTextService, mediaNavigationQueryService));

Validators.Add(validators);
Expand Down Expand Up @@ -401,18 +402,22 @@
/// </summary>
internal sealed class AllowedTypeValidator : ITypedJsonValidator<List<MediaWithCropsDto>, MediaPicker3Configuration>
{
private const string MediaTypeCacheKeyFormat = nameof(AllowedTypeValidator) + "_MediaTypeKey_{0}";

private readonly ILocalizedTextService _localizedTextService;
private readonly IMediaTypeService _mediaTypeService;
private readonly IMediaService _mediaService;
private readonly AppCaches _appCaches;

/// <summary>
/// Initializes a new instance of the <see cref="AllowedTypeValidator"/> class.
/// </summary>
public AllowedTypeValidator(ILocalizedTextService localizedTextService, IMediaTypeService mediaTypeService, IMediaService mediaService)
public AllowedTypeValidator(ILocalizedTextService localizedTextService, IMediaTypeService mediaTypeService, IMediaService mediaService, AppCaches appCaches)
{
_localizedTextService = localizedTextService;
_mediaTypeService = mediaTypeService;
_mediaService = mediaService;
_appCaches = appCaches;
}

/// <inheritdoc/>
Expand Down Expand Up @@ -452,9 +457,20 @@

foreach (var typeAlias in distinctTypeAliases)
{
IMediaType? type = _mediaTypeService.Get(typeAlias);
// Cache media type lookups since the same media type is likely to be used multiple times in validation,
// particularly if we have multiple languages and blocks.
var cacheKey = string.Format(MediaTypeCacheKeyFormat, typeAlias);
string? typeKey = _appCaches.RequestCache.GetCacheItem<string?>(cacheKey);
if (typeKey is null)
{
typeKey = _mediaTypeService.Get(typeAlias)?.Key.ToString();
if (typeKey is not null)
{
_appCaches.RequestCache.Set(cacheKey, typeKey);
}
}

if (type is null || allowedTypes.Contains(type.Key.ToString()) is false)
if (typeKey is null || allowedTypes.Contains(typeKey) is false)

Check warning on line 473 in src/Umbraco.Infrastructure/PropertyEditors/MediaPicker3PropertyEditor.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Code Health Review (main)

❌ Getting worse: Complex Method

Validate increases in cyclomatic complexity from 9 to 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
{
return
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class DataValueReferenceFactoryCollectionTests
Mock.Of<IDataTypeConfigurationCache>(),
Mock.Of<ILocalizedTextService>(),
Mock.Of<IMediaTypeService>(),
Mock.Of<IMediaNavigationQueryService>()));
Mock.Of<IMediaNavigationQueryService>(),
AppCaches.Disabled));

private IIOHelper IOHelper { get; } = Mock.Of<IIOHelper>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ private static (MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor Value
Mock.Of<IDataTypeConfigurationCache>(),
Mock.Of<ILocalizedTextService>(),
mediaTypeServiceMock.Object,
mediaNavigationQueryServiceMock.Object)
mediaNavigationQueryServiceMock.Object,
AppCaches.Disabled)
{
ConfigurationObject = new MediaPicker3Configuration()
};
Expand Down