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
37 changes: 17 additions & 20 deletions src/Umbraco.Core/Services/ContentService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;

Check notice on line 1 in src/Umbraco.Core/Services/ContentService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 2281 to 2276, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in src/Umbraco.Core/Services/ContentService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 4.20 to 4.14, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.

Check notice on line 1 in src/Umbraco.Core/Services/ContentService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 46.96% to 46.52%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
using System.Runtime.InteropServices;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -1108,14 +1108,8 @@

if (culturesChanging != null)
{
IEnumerable<string>? languages = _languageRepository.GetMany()?
.Where(x => culturesChanging.InvariantContains(x.IsoCode))
.Select(x => x.CultureName);
if (languages is not null)
{
var langs = string.Join(", ", languages);
Audit(AuditType.SaveVariant, userId.Value, content.Id, $"Saved languages: {langs}", langs);
}
var langs = GetLanguageDetailsForAuditEntry(culturesChanging);
Audit(AuditType.SaveVariant, userId.Value, content.Id, $"Saved languages: {langs}", langs);

Check notice on line 1112 in src/Umbraco.Core/Services/ContentService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Complex Method

Save decreases in cyclomatic complexity from 12 to 11, 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.
}
else
{
Expand Down Expand Up @@ -1625,9 +1619,7 @@
if (culturesUnpublishing != null)
{
// This will mean that that we unpublished a mandatory culture or we unpublished the last culture.
var langs = string.Join(", ", allLangs
.Where(x => culturesUnpublishing.InvariantContains(x.IsoCode))
.Select(x => x.CultureName));
var langs = GetLanguageDetailsForAuditEntry(allLangs, culturesUnpublishing);
Audit(AuditType.UnpublishVariant, userId, content.Id, $"Unpublished languages: {langs}", langs);

if (publishResult == null)
Expand Down Expand Up @@ -1707,19 +1699,15 @@
case PublishResultType.SuccessPublishCulture:
if (culturesPublishing != null)
{
var langs = string.Join(", ", allLangs
.Where(x => culturesPublishing.InvariantContains(x.IsoCode))
.Select(x => x.CultureName));
var langs = GetLanguageDetailsForAuditEntry(allLangs, culturesPublishing);
Audit(AuditType.PublishVariant, userId, content.Id, $"Published languages: {langs}", langs);
}

break;
case PublishResultType.SuccessUnpublishCulture:
if (culturesUnpublishing != null)
{
var langs = string.Join(", ", allLangs
.Where(x => culturesUnpublishing.InvariantContains(x.IsoCode))
.Select(x => x.CultureName));
var langs = GetLanguageDetailsForAuditEntry(allLangs, culturesUnpublishing);
Audit(AuditType.UnpublishVariant, userId, content.Id, $"Unpublished languages: {langs}", langs);
}

Expand All @@ -1741,9 +1729,7 @@
{
if (culturesChanging != null)
{
var langs = string.Join(", ", allLangs
.Where(x => culturesChanging.InvariantContains(x.IsoCode))
.Select(x => x.CultureName));
var langs = GetLanguageDetailsForAuditEntry(allLangs, culturesChanging);
Audit(AuditType.SaveVariant, userId, content.Id, $"Saved languages: {langs}", langs);
}
else
Expand Down Expand Up @@ -3098,6 +3084,17 @@
private void Audit(AuditType type, int userId, int objectId, string? message = null, string? parameters = null) =>
_auditRepository.Save(new AuditItem(objectId, type, userId, UmbracoObjectTypes.Document.GetName(), message, parameters));

private string GetLanguageDetailsForAuditEntry(IEnumerable<string> affectedCultures)
=> GetLanguageDetailsForAuditEntry(_languageRepository.GetMany(), affectedCultures);

private static string GetLanguageDetailsForAuditEntry(IEnumerable<ILanguage> languages, IEnumerable<string> affectedCultures)
{
IEnumerable<string> languageIsoCodes = languages
.Where(x => affectedCultures.InvariantContains(x.IsoCode))
.Select(x => x.IsoCode);
return string.Join(", ", languageIsoCodes);
}

private static bool IsDefaultCulture(IReadOnlyCollection<ILanguage>? langs, string culture) =>
langs?.Any(x => x.IsDefault && x.IsoCode.InvariantEquals(culture)) ?? false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentServiceTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/16.0)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 2507 to 2506, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
// See LICENSE for more details.

using System.Diagnostics;
Expand Down Expand Up @@ -59,8 +59,6 @@

private IRelationService RelationService => GetRequiredService<IRelationService>();

private ILocalizedTextService TextService => GetRequiredService<ILocalizedTextService>();

private ITagService TagService => GetRequiredService<ITagService>();

private IPublicAccessService PublicAccessService => GetRequiredService<IPublicAccessService>();
Expand Down Expand Up @@ -738,8 +736,7 @@
[Test]
public void Can_Unpublish_Content_Variation()
{
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr,
out var contentType);
var content = CreateEnglishAndFrenchDocument(out var langUk, out var langFr, out var contentType);

var saved = ContentService.Save(content);
var published = ContentService.Publish(content, new[] { langFr.IsoCode, langUk.IsoCode });
Expand Down Expand Up @@ -1032,7 +1029,7 @@

// audit log will only show that french was published
var lastLog = AuditService.GetLogs(content.Id).Last();
Assert.AreEqual("Published languages: French (France)", lastLog.Comment);
Assert.AreEqual("Published languages: fr-FR", lastLog.Comment);

// re-get
content = ContentService.GetById(content.Id);
Expand All @@ -1042,7 +1039,7 @@

// audit log will only show that english was published
lastLog = AuditService.GetLogs(content.Id).Last();
Assert.AreEqual("Published languages: English (United Kingdom)", lastLog.Comment);
Assert.AreEqual("Published languages: en-GB", lastLog.Comment);
}

[Test]
Expand Down Expand Up @@ -1079,7 +1076,7 @@

// audit log will only show that french was unpublished
var lastLog = AuditService.GetLogs(content.Id).Last();
Assert.AreEqual("Unpublished languages: French (France)", lastLog.Comment);
Assert.AreEqual("Unpublished languages: fr-FR", lastLog.Comment);

// re-get
content = ContentService.GetById(content.Id);
Expand All @@ -1088,7 +1085,7 @@

// audit log will only show that english was published
var logs = AuditService.GetLogs(content.Id).ToList();
Assert.AreEqual("Unpublished languages: English (United Kingdom)", logs[^2].Comment);
Assert.AreEqual("Unpublished languages: en-GB", logs[^2].Comment);
Assert.AreEqual("Unpublished (mandatory language unpublished)", logs[^1].Comment);
}

Expand Down
Loading