< Summary

Information
Class: Elsa.Workflows.Management.Services.WorkflowInstanceManager
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowInstanceManager.cs
Line coverage
54%
Covered lines: 39
Uncovered lines: 32
Coverable lines: 71
Total lines: 155
Line coverage: 54.9%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
FindByIdAsync()100%11100%
FindAsync()100%210%
ExistsAsync()100%210%
SaveAsync()100%11100%
SaveAsync()100%11100%
SaveAsync()100%210%
CreateAsync()100%210%
CreateAsync()100%210%
CreateAsync()100%210%
UpdateAsync()100%210%
UpdateAsync()100%210%
DeleteAsync()50%2291.66%
BulkDeleteAsync()100%11100%
ExtractWorkflowState(...)100%210%
SerializeWorkflowState(...)100%210%
CreateAndCommitWorkflowInstanceAsync()100%11100%
CreateWorkflowInstance(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowInstanceManager.cs

#LineLine coverage
 1using Elsa.Extensions;
 2using Elsa.Mediator.Contracts;
 3using Elsa.Workflows.Activities;
 4using Elsa.Workflows.Management.Contracts;
 5using Elsa.Workflows.Management.Entities;
 6using Elsa.Workflows.Management.Filters;
 7using Elsa.Workflows.Management.Mappers;
 8using Elsa.Workflows.Management.Notifications;
 9using Elsa.Workflows.Management.Options;
 10using Elsa.Workflows.State;
 11
 12namespace Elsa.Workflows.Management.Services;
 13
 14/// <inheritdoc />
 32115public class WorkflowInstanceManager(
 32116    IWorkflowInstanceStore store,
 32117    IWorkflowInstanceFactory workflowInstanceFactory,
 32118    INotificationSender notificationSender,
 32119    WorkflowStateMapper workflowStateMapper,
 32120    IWorkflowStateExtractor workflowStateExtractor,
 32121    IWorkflowStateSerializer workflowStateSerializer)
 22    : IWorkflowInstanceManager
 23{
 24    /// <inheritdoc />
 25    public async Task<WorkflowInstance?> FindByIdAsync(string instanceId, CancellationToken cancellationToken = default)
 26    {
 7627        return await store.FindAsync(instanceId, cancellationToken);
 7628    }
 29
 30    /// <inheritdoc />
 31    public async Task<WorkflowInstance?> FindAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = 
 32    {
 033        return await store.FindAsync(filter, cancellationToken);
 034    }
 35
 36    public async Task<bool> ExistsAsync(string instanceId, CancellationToken cancellationToken = default)
 37    {
 038        var filter = new WorkflowInstanceFilter
 039        {
 040            Id = instanceId
 041        };
 042        var count = await store.CountAsync(filter, cancellationToken);
 043        return count > 0;
 044    }
 45
 46    /// <inheritdoc />
 47    public async Task SaveAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default)
 48    {
 34949        await store.SaveAsync(workflowInstance, cancellationToken);
 34950        await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken);
 34951    }
 52
 53    /// <inheritdoc />
 54    public async Task<WorkflowInstance> SaveAsync(WorkflowState workflowState, CancellationToken cancellationToken)
 55    {
 32756        var workflowInstance = workflowStateMapper.Map(workflowState)!;
 32757        await SaveAsync(workflowInstance, cancellationToken);
 32758        return workflowInstance;
 32759    }
 60
 61    /// <inheritdoc />
 62    public async Task<WorkflowInstance> SaveAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken c
 63    {
 064        var workflowState = ExtractWorkflowState(workflowExecutionContext);
 065        return await SaveAsync(workflowState, cancellationToken);
 066    }
 67
 68    public async Task CreateAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default)
 69    {
 070        await store.AddAsync(workflowInstance, cancellationToken);
 071        await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken);
 072    }
 73
 74    public async Task<WorkflowInstance> CreateAsync(WorkflowState workflowState, CancellationToken cancellationToken)
 75    {
 076        var workflowInstance = workflowStateMapper.Map(workflowState)!;
 077        await CreateAsync(workflowInstance, cancellationToken);
 078        return workflowInstance;
 079    }
 80
 81    public async Task<WorkflowInstance> CreateAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken
 82    {
 083        var workflowState = ExtractWorkflowState(workflowExecutionContext);
 084        return await CreateAsync(workflowState, cancellationToken);
 085    }
 86
 87    /// <inheritdoc />
 88    public async Task UpdateAsync(WorkflowInstance workflowInstance, CancellationToken cancellationToken = default)
 89    {
 090        await store.UpdateAsync(workflowInstance, cancellationToken);
 091        await notificationSender.SendAsync(new WorkflowInstanceSaved(workflowInstance), cancellationToken);
 092    }
 93
 94    public async Task<WorkflowInstance> UpdateAsync(WorkflowState workflowState, CancellationToken cancellationToken)
 95    {
 096        var workflowInstance = workflowStateMapper.Map(workflowState)!;
 097        await UpdateAsync(workflowInstance, cancellationToken);
 098        return workflowInstance;
 099    }
 100
 101    /// <inheritdoc />
 102    public async Task<bool> DeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default)
 103    {
 7104        var instance = await store.FindAsync(filter, cancellationToken);
 105
 7106        if (instance == null)
 0107            return false;
 108
 7109        var ids = new[]
 7110        {
 7111            instance.Id
 7112        };
 7113        await notificationSender.SendAsync(new WorkflowInstancesDeleting(ids), cancellationToken);
 7114        await store.DeleteAsync(filter, cancellationToken);
 7115        await notificationSender.SendAsync(new WorkflowInstancesDeleted(ids), cancellationToken);
 7116        return true;
 7117    }
 118
 119    /// <inheritdoc />
 120    public async Task<long> BulkDeleteAsync(WorkflowInstanceFilter filter, CancellationToken cancellationToken = default
 121    {
 6122        var summaries = (await store.SummarizeManyAsync(filter, cancellationToken)).ToList();
 9123        var ids = summaries.Select(x => x.Id).ToList();
 6124        await notificationSender.SendAsync(new WorkflowInstancesDeleting(ids), cancellationToken);
 6125        var count = await store.DeleteAsync(filter, cancellationToken);
 6126        await notificationSender.SendAsync(new WorkflowInstancesDeleted(ids), cancellationToken);
 6127        return count;
 6128    }
 129
 130    /// <inheritdoc />
 131    public WorkflowState ExtractWorkflowState(WorkflowExecutionContext workflowExecutionContext)
 132    {
 0133        return workflowStateExtractor.Extract(workflowExecutionContext);
 134    }
 135
 136    /// <inheritdoc />
 137    public string SerializeWorkflowState(WorkflowState workflowState)
 138    {
 0139        return workflowStateSerializer.Serialize(workflowState);
 140    }
 141
 142    /// <inheritdoc />
 143    public async Task<WorkflowInstance> CreateAndCommitWorkflowInstanceAsync(Workflow workflow, WorkflowInstanceOptions?
 144    {
 22145        var workflowInstance = CreateWorkflowInstance(workflow, options);
 22146        await SaveAsync(workflowInstance, cancellationToken);
 22147        return workflowInstance;
 22148    }
 149
 150    /// <inheritdoc />
 151    public WorkflowInstance CreateWorkflowInstance(Workflow workflow, WorkflowInstanceOptions? options = null)
 152    {
 72153        return workflowInstanceFactory.CreateWorkflowInstance(workflow, options);
 154    }
 155}