< Summary - Envilder .NET SDK

Information
Class: Envilder.Infrastructure.DependencyInjection.ServiceCollectionExtensions
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs
Tag: 151_24479375065
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddEnvilder(...)100%66100%

File(s)

/home/runner/work/envilder/envilder/src/sdks/dotnet/Infrastructure/DependencyInjection/ServiceCollectionExtensions.cs

#LineLine coverage
 1namespace Envilder.Infrastructure.DependencyInjection;
 2
 3using Envilder.Application;
 4using Envilder.Domain;
 5using Envilder.Domain.Ports;
 6using Microsoft.Extensions.DependencyInjection;
 7using System;
 8using System.IO;
 9
 10/// <summary>
 11/// Extension methods for registering Envilder services in an
 12/// <see cref="IServiceCollection"/> dependency injection container.
 13/// </summary>
 14public static class ServiceCollectionExtensions
 15{
 16    /// <summary>
 17    /// Parses the map file at <paramref name="mapFilePath"/>, creates an
 18    /// <see cref="EnvilderClient"/> backed by <paramref name="secretProvider"/>,
 19    /// and registers both the client and the parsed <see cref="ParsedMapFile"/>
 20    /// as singleton services.
 21    /// </summary>
 22    /// <param name="services">The service collection.</param>
 23    /// <param name="mapFilePath">Path to the JSON map file on disk.</param>
 24    /// <param name="secretProvider">The secret provider to resolve values from.</param>
 25    /// <returns>The same <see cref="IServiceCollection"/> for chaining.</returns>
 26    /// <exception cref="ArgumentException"><paramref name="mapFilePath"/> is null or empty.</exception>
 27    /// <exception cref="FileNotFoundException">The map file does not exist on disk.</exception>
 28    public static IServiceCollection AddEnvilder(this IServiceCollection services,
 29                                                 string mapFilePath,
 30                                                 ISecretProvider secretProvider)
 31    {
 132        if (string.IsNullOrWhiteSpace(mapFilePath))
 33        {
 134            throw new ArgumentException("Map file path cannot be null or empty.", nameof(mapFilePath));
 35        }
 36
 137        if (!File.Exists(mapFilePath))
 38        {
 139            throw new FileNotFoundException("Map file not found.", mapFilePath);
 40        }
 41
 142        if (secretProvider is null)
 43        {
 144            throw new ArgumentNullException(nameof(secretProvider));
 45        }
 46
 147        var json = File.ReadAllText(mapFilePath);
 148        var mapFile = new MapFileParser().Parse(json);
 49
 150        services.AddSingleton(mapFile);
 151        services.AddSingleton(new EnvilderClient(secretProvider));
 52
 153        return services;
 54    }
 55}