< Summary

Information
Class: Elsa.Alterations.Core.Contexts.AlterationContext
Assembly: Elsa.Alterations.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations.Core/Contexts/AlterationHandlerContext.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 49
Coverable lines: 49
Total lines: 155
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_Alteration()100%210%
get_WorkflowExecutionContext()100%210%
get_Workflow()100%210%
get_CancellationToken()100%210%
get_ServiceProvider()100%210%
get_AlterationLog()100%210%
get_HasSucceeded()100%210%
get_HasFailed()100%210%
get_CommitAction()100%210%
Log(...)100%210%
Succeed()100%210%
Succeed(...)100%210%
Succeed(...)100%210%
Succeed(...)100%210%
Succeed(...)100%210%
Succeed(...)100%210%
Fail(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations.Core/Contexts/AlterationHandlerContext.cs

#LineLine coverage
 1using Elsa.Alterations.Core.Contracts;
 2using Elsa.Alterations.Core.Models;
 3using Elsa.Workflows;
 4using Elsa.Workflows.Activities;
 5using Microsoft.Extensions.Logging;
 6
 7namespace Elsa.Alterations.Core.Contexts;
 8
 9/// <summary>
 10/// Provides contextual information about an alteration.
 11/// </summary>
 12public class AlterationContext
 13{
 14    /// <summary>
 15    /// Initializes a new instance of the <see cref="AlterationContext"/> class.
 16    /// </summary>
 017    public AlterationContext(
 018        IAlteration alteration,
 019        WorkflowExecutionContext workflowExecutionContext,
 020        AlterationLog log,
 021        CancellationToken cancellationToken)
 22    {
 023        Alteration = alteration;
 024        WorkflowExecutionContext = workflowExecutionContext;
 025        AlterationLog = log;
 026        CancellationToken = cancellationToken;
 027    }
 28
 29    /// <summary>
 30    /// The alteration being handled.
 31    /// </summary>
 032    public IAlteration Alteration { get; }
 33
 34    /// <summary>
 35    /// A workflow execution context of the workflow instance being altered. This offers maximum flexibility for alterin
 36    /// </summary>
 037    public WorkflowExecutionContext WorkflowExecutionContext { get; set; }
 38
 39    /// <summary>
 40    /// The workflow of the workflow instance being altered.
 41    /// </summary>
 042    public Workflow Workflow => WorkflowExecutionContext.Workflow;
 43
 44    /// <summary>
 45    /// The cancellation token.
 46    /// </summary>
 047    public CancellationToken CancellationToken { get; }
 48
 49    /// <summary>
 50    /// The service provider.
 51    /// </summary>
 052    public IServiceProvider ServiceProvider => WorkflowExecutionContext.ServiceProvider;
 53
 54    /// <summary>
 55    /// The alteration log.
 56    /// </summary>
 057    public AlterationLog AlterationLog { get; }
 58
 59    /// <summary>
 60    /// A flag indicating whether the alteration has succeeded.
 61    /// </summary>
 062    public bool HasSucceeded { get; private set; }
 63
 64    /// <summary>
 65    /// A flag indicating whether the alteration has failed.
 66    /// </summary>
 067    public bool HasFailed { get; private set; }
 68
 69    /// <summary>
 70    /// An optional action to be executed when the alteration is committed. Set this to perform permanent side effects s
 71    /// </summary>
 072    public Func<Task>? CommitAction { get; set; }
 73
 74    /// <summary>
 75    /// Logs a message.
 76    /// </summary>
 77    /// <param name="eventName">The event name to log.</param>
 78    /// <param name="message">The message to log.</param>
 79    /// <param name="logLevel">The log level.</param>
 80    public void Log(string eventName, string message, LogLevel logLevel = LogLevel.Information)
 81    {
 082        AlterationLog.Add(message, logLevel, eventName);
 083    }
 84
 85    /// <summary>
 86    /// Marks the alteration as succeeded.
 87    /// </summary>
 88    public void Succeed()
 89    {
 090        Succeed($"{Alteration.GetType().Name} succeeded");
 091    }
 92
 93    /// <summary>
 94    /// Marks the alteration as succeeded.
 95    /// </summary>
 96    public void Succeed(Func<Task> commitAction)
 97    {
 098        Succeed();
 099        CommitAction = commitAction;
 0100    }
 101
 102    /// <summary>
 103    /// Marks the alteration as succeeded.
 104    /// </summary>
 105    public void Succeed(Action commitAction)
 106    {
 0107        Succeed();
 0108        CommitAction = () =>
 0109        {
 0110            commitAction();
 0111            return Task.CompletedTask;
 0112        };
 0113    }
 114
 115    /// <summary>
 116    /// Marks the alteration as succeeded.
 117    /// </summary>
 118    public void Succeed(string message)
 119    {
 0120        HasSucceeded = true;
 0121        Log($"Alteration {Alteration.GetType().Name} succeeded", message);
 0122    }
 123
 124    /// <summary>
 125    /// Marks the alteration as succeeded.
 126    /// </summary>
 127    public void Succeed(string message, Func<Task> commitAction)
 128    {
 0129        Succeed(message);
 0130        CommitAction = commitAction;
 0131    }
 132
 133    /// <summary>
 134    /// Marks the alteration as succeeded.
 135    /// </summary>
 136    public void Succeed(string message, Action commitAction)
 137    {
 0138        Succeed(message);
 0139        CommitAction = () =>
 0140        {
 0141            commitAction();
 0142            return Task.CompletedTask;
 0143        };
 0144    }
 145
 146    /// <summary>
 147    /// Marks the alteration as failed.
 148    /// </summary>
 149    /// <param name="message">An optional message.</param>
 150    public void Fail(string? message = null)
 151    {
 0152        HasFailed = true;
 0153        Log($"Alteration {Alteration.GetType().Name} failed", message ?? $"{Alteration.GetType().Name} failed", LogLevel
 0154    }
 155}