< Summary

Information
Class: Elsa.Alterations.AlterationHandlers.ModifyVariableHandler
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/ModifyVariableHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 74
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleAsync()0%620%
UpdateVariable(...)0%620%
FindActivityExecutionContextContainingVariable(...)100%210%
FindVariable()100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/ModifyVariableHandler.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using Elsa.Alterations.AlterationTypes;
 3using Elsa.Alterations.Core.Abstractions;
 4using Elsa.Alterations.Core.Contexts;
 5using Elsa.Extensions;
 6using Elsa.Workflows;
 7using Elsa.Workflows.Activities;
 8using Elsa.Workflows.Memory;
 9using JetBrains.Annotations;
 10using Microsoft.Extensions.DependencyInjection;
 11
 12namespace Elsa.Alterations.AlterationHandlers;
 13
 14/// <summary>
 15/// Modifies a workflow variable.
 16/// </summary>
 17[UsedImplicitly]
 18public class ModifyVariableHandler : AlterationHandlerBase<ModifyVariable>
 19{
 20    /// <inheritdoc />
 21    [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
 22    protected override async ValueTask HandleAsync(AlterationContext context, ModifyVariable alteration)
 23    {
 024        var workflow = context.Workflow;
 025        var cancellationToken = context.CancellationToken;
 026        var variable = await FindVariable(context, alteration, workflow, cancellationToken);
 27
 028        if (variable == null)
 29        {
 030            context.Fail($"Variable with ID {alteration.VariableId} not found");
 031            return;
 32        }
 33
 034        var convertedValue = variable.ParseValue(alteration.Value);
 035        UpdateVariable(context, variable, convertedValue);
 036        context.Succeed();
 037    }
 38
 39    private void UpdateVariable(AlterationContext context, Variable variable, object? value)
 40    {
 041        var activityExecutionContext = FindActivityExecutionContextContainingVariable(context, variable);
 42
 043        if (activityExecutionContext == null)
 44        {
 045            context.Fail($"Activity execution context containing variable with ID {variable.Id} not found");
 046            return;
 47        }
 48
 049        variable.Set(activityExecutionContext, value);
 050    }
 51
 52    private ActivityExecutionContext? FindActivityExecutionContextContainingVariable(AlterationContext context, Variable
 53    {
 054        var query =
 055            from activityExecutionContext in context.WorkflowExecutionContext.ActivityExecutionContexts
 056            from var in activityExecutionContext.Variables
 057            where var.Id == variable.Id
 058            select activityExecutionContext;
 59
 060        return query.FirstOrDefault();
 61    }
 62
 63    private async Task<Variable?> FindVariable(AlterationContext context, ModifyVariable alteration, Workflow workflow, 
 64    {
 065        var activityVisitor = context.ServiceProvider.GetRequiredService<IActivityVisitor>();
 066        var graph = await activityVisitor.VisitAsync(workflow, cancellationToken);
 067        var flattenedList = graph.Flatten().ToList();
 68
 069        return flattenedList
 070            .Where(x => x.Activity is IVariableContainer)
 071            .SelectMany(x => ((IVariableContainer)x.Activity).Variables)
 072            .FirstOrDefault(x => x.Id == alteration.VariableId);
 073    }
 74}