< Summary

Information
Class: Elsa.Tenants.Providers.ConfigurationTenantsProvider
Assembly: Elsa.Tenants
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants/Providers/ConfigurationTenantsProvider.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 70
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%
OnOptionsChanged()100%210%
UpdateTenants(...)0%620%
ListAsync(...)100%210%
FindAsync(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Tenants/Providers/ConfigurationTenantsProvider.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Tenants.Options;
 3using JetBrains.Annotations;
 4using Microsoft.Extensions.Configuration;
 5using Microsoft.Extensions.Logging;
 6using Microsoft.Extensions.Options;
 7
 8namespace Elsa.Tenants.Providers;
 9
 10/// <summary>
 11/// Provides the implementation to retrieve tenant information from a configuration.
 12/// </summary>
 13[UsedImplicitly]
 14public class ConfigurationTenantsProvider : ITenantsProvider
 15{
 16    private readonly IConfiguration _configuration;
 17    private readonly ITenantService _tenantService;
 18    private readonly ILogger<ConfigurationTenantsProvider> _logger;
 019    private ICollection<Tenant> _tenants = new List<Tenant>();
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="ConfigurationTenantsProvider"/> class.
 23    /// </summary>
 024    public ConfigurationTenantsProvider(IOptionsMonitor<TenantsOptions> options, IConfiguration configuration, ITenantSe
 25    {
 026        _configuration = configuration;
 027        _tenantService = tenantService;
 028        _logger = logger;
 029        UpdateTenants(options.CurrentValue);
 030        options.OnChange(OnOptionsChanged);
 031    }
 32
 33    private async void OnOptionsChanged(TenantsOptions options, string? name)
 34    {
 35        try
 36        {
 037            UpdateTenants(options);
 038            await _tenantService.RefreshAsync();
 039        }
 040        catch (Exception e)
 41        {
 042            _logger.LogError(e, "An error occurred while updating tenants.");
 043        }
 044    }
 45
 46    private void UpdateTenants(TenantsOptions options)
 47    {
 048        var tenants = options.Tenants.ToList();
 49
 50        // Rebind each Tenant's Configuration property manually using array indices
 051        for (int i = 0; i < tenants.Count; i++)
 052            tenants[i].Configuration = _configuration.GetSection($"Multitenancy:Tenants:{i}:Configuration");
 53
 054        _tenants = tenants;
 055    }
 56
 57    /// <inheritdoc />
 58    public Task<IEnumerable<Tenant>> ListAsync(CancellationToken cancellationToken = default)
 59    {
 060        return Task.FromResult<IEnumerable<Tenant>>(_tenants);
 61    }
 62
 63    /// <inheritdoc />
 64    public Task<Tenant?> FindAsync(TenantFilter filter, CancellationToken cancellationToken = default)
 65    {
 066        var tenantsQueryable = _tenants.AsQueryable();
 067        var tenant = filter.Apply(tenantsQueryable).FirstOrDefault();
 068        return Task.FromResult(tenant);
 69    }
 70}