< Summary

Information
Class: TeleFlow.Telegram.Webhooks.TelegramWebhookServiceCollectionExtensions
Assembly: TeleFlow.Framework.Webhooks
File(s): /_/src/TeleFlow.Framework.Webhooks/TelegramWebhookServiceCollectionExtensions.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 50
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

/_/src/TeleFlow.Framework.Webhooks/TelegramWebhookServiceCollectionExtensions.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3using Microsoft.Extensions.Hosting;
 4using TeleFlow.Framework.Updates;
 5using TeleFlow.Telegram.Internal;
 6using TeleFlow.Telegram.Webhooks.Internal;
 7
 8namespace TeleFlow.Telegram.Webhooks;
 9
 10public static class TelegramWebhookServiceCollectionExtensions
 11{
 12    public static IServiceCollection AddWebhook(
 13        this IServiceCollection services,
 14        Action<TelegramWebhookOptions>? configure = null)
 15    {
 1916        ArgumentNullException.ThrowIfNull(services);
 17
 1918        EnsureNotRegistered<IUpdateSource>(services, nameof(AddWebhook));
 1819        EnsureTelegramBotRegistered(services, nameof(AddWebhook));
 20
 1721        var options = new TelegramWebhookOptions();
 1722        configure?.Invoke(options);
 1723        TelegramWebhookOptionsValidator.Validate(options);
 24
 1525        services.AddSingleton(options);
 1526        services.TryAddSingleton<IUpdateProcessor, DefaultUpdateProcessor>();
 1527        services.AddSingleton<IUpdateSource, TelegramWebhookUpdateSource>();
 1528        services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, TelegramBotIdentityStartupService>());
 29
 1530        return services;
 31    }
 32
 33    private static void EnsureTelegramBotRegistered(IServiceCollection services, string apiName)
 34    {
 128835        if (services.All(static descriptor => descriptor.ServiceType != typeof(TelegramBotOptions)))
 36        {
 137            throw new InvalidOperationException(
 138                $"{nameof(TelegramServiceCollectionExtensions.AddTelegramBot)} must be called before {apiName}.");
 39        }
 1740    }
 41
 42    private static void EnsureNotRegistered<TService>(IServiceCollection services, string apiName)
 43    {
 157844        if (services.Any(descriptor => descriptor.ServiceType == typeof(TService)))
 45        {
 146            throw new InvalidOperationException(
 147                $"{apiName} cannot be called more than once for service '{typeof(TService).Name}'.");
 48        }
 1849    }
 50}