< Summary

Information
Class: TeleFlow.Telegram.TelegramLongPollingServiceCollectionExtensions
Assembly: TeleFlow.Framework.LongPolling
File(s): /_/src/TeleFlow.Framework.LongPolling/TelegramLongPollingServiceCollectionExtensions.cs
Line coverage
88%
Covered lines: 16
Uncovered lines: 2
Coverable lines: 18
Total lines: 47
Line coverage: 88.8%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddLongPolling(...)100%22100%
EnsureTelegramBotRegistered(...)50%3250%
EnsureNotRegistered(...)100%44100%

File(s)

/_/src/TeleFlow.Framework.LongPolling/TelegramLongPollingServiceCollectionExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using TeleFlow.Framework.Updates;
 3using TeleFlow.Telegram.Internal;
 4using TeleFlow.Telegram.Internal.Options;
 5
 6namespace TeleFlow.Telegram;
 7
 8public static class TelegramLongPollingServiceCollectionExtensions
 9{
 10    public static IServiceCollection AddLongPolling(
 11        this IServiceCollection services,
 12        Action<TelegramLongPollingOptions>? configure = null)
 13    {
 2714        ArgumentNullException.ThrowIfNull(services);
 15
 2716        EnsureNotRegistered<IUpdateSource>(services, nameof(AddLongPolling));
 2517        EnsureTelegramBotRegistered(services, nameof(AddLongPolling));
 18
 2519        var options = new TelegramLongPollingOptions();
 2520        configure?.Invoke(options);
 2521        TelegramLongPollingOptionsValidator.Validate(options);
 22
 1723        services.AddTelegramLongPollingClient();
 1724        services.AddSingleton(options);
 1725        services.AddSingleton<IUpdateSource, TelegramLongPollingUpdateSource>();
 26
 1727        return services;
 28    }
 29
 30    private static void EnsureTelegramBotRegistered(IServiceCollection services, string apiName)
 31    {
 35532        if (services.All(static descriptor => descriptor.ServiceType != typeof(TelegramBotOptions)))
 33        {
 034            throw new InvalidOperationException(
 035                $"{nameof(TelegramServiceCollectionExtensions.AddTelegramBot)} must be called before {apiName}.");
 36        }
 2537    }
 38
 39    private static void EnsureNotRegistered<TService>(IServiceCollection services, string apiName)
 40    {
 69541        if (services.Any(descriptor => descriptor.ServiceType == typeof(TService)))
 42        {
 243            throw new InvalidOperationException(
 244                $"{apiName} cannot be called more than once for service '{typeof(TService).Name}'.");
 45        }
 2546    }
 47}