< Summary

Information
Class: TeleFlow.Telegram.Internal.Handlers.TelegramHandlerInvoker
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/Handlers/TelegramHandlerInvoker.cs
Line coverage
76%
Covered lines: 40
Uncovered lines: 12
Coverable lines: 52
Total lines: 116
Line coverage: 76.9%
Branch coverage
81%
Covered branches: 26
Total branches: 32
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
InvokeAsync()81.25%181681.81%
GetRouteValue(...)81.25%241668.42%

File(s)

/_/src/TeleFlow.Framework/Internal/Handlers/TelegramHandlerInvoker.cs

#LineLine coverage
 1using System.Reflection;
 2using System.Runtime.ExceptionServices;
 3using Microsoft.Extensions.DependencyInjection;
 4
 5namespace TeleFlow.Telegram.Internal.Handlers;
 6
 7internal static class TelegramHandlerInvoker
 8{
 9    public static async Task InvokeAsync(
 10        TelegramRouteSelection selection,
 11        TelegramUpdateContext context,
 12        CancellationToken cancellationToken)
 13    {
 32214        ArgumentNullException.ThrowIfNull(selection);
 15
 32216        var handler = selection.Handler;
 32217        var arguments = new object?[handler.Parameters.Count];
 18
 196419        for (var index = 0; index < handler.Parameters.Count; index++)
 20        {
 66121            var parameter = handler.Parameters[index];
 22
 66123            arguments[index] = parameter.Kind switch
 66124            {
 32225                TelegramHandlerParameterKind.Context => context,
 926                TelegramHandlerParameterKind.CallbackPayload => selection.CallbackPayload,
 2727                TelegramHandlerParameterKind.RouteValue => GetRouteValue(selection, parameter),
 28728                TelegramHandlerParameterKind.Service => context.Services.GetRequiredService(parameter.ParameterType),
 1629                TelegramHandlerParameterKind.CancellationToken => cancellationToken,
 030                _ => throw new InvalidOperationException($"Unsupported handler parameter kind '{parameter.Kind}'.")
 66131            };
 32        }
 33
 32134        if (handler.GeneratedInvoker is not null)
 35        {
 5636            await handler.GeneratedInvoker(context.Services, arguments, cancellationToken).ConfigureAwait(false);
 5337            return;
 38        }
 39
 26540        if (handler.Method is null)
 41        {
 042            throw new InvalidOperationException(
 043                $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(handler)}' has no invoker.");
 44        }
 45
 26546        var handlerInstance = context.Services.GetRequiredService(handler.HandlerType);
 47        object? result;
 48
 49        try
 50        {
 26551            result = handler.Method.Invoke(handlerInstance, arguments);
 26252        }
 353        catch (TargetInvocationException exception) when (exception.InnerException is not null)
 54        {
 355            ExceptionDispatchInfo.Capture(exception.InnerException).Throw();
 056            throw;
 57        }
 58
 59        switch (result)
 60        {
 61            case Task task:
 26162                await task.ConfigureAwait(false);
 24363                break;
 64            case ValueTask valueTask:
 165                await valueTask.ConfigureAwait(false);
 166                break;
 67            default:
 068                throw new InvalidOperationException(
 069                    $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(handler)}' returned an unsupp
 70        }
 29771    }
 72
 73    private static object? GetRouteValue(
 74        TelegramRouteSelection selection,
 75        TelegramHandlerParameterDescriptor parameter)
 76    {
 2777        if (string.IsNullOrWhiteSpace(parameter.Name))
 78        {
 079            throw new InvalidOperationException(
 080                $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(selection.Handler)}' has an unnam
 81        }
 82
 2783        if (!selection.RouteValues.TryGetValue(parameter.Name, out var value))
 84        {
 185            throw new InvalidOperationException(
 186                $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(selection.Handler)}' requires rou
 87        }
 88
 2689        if (value is null)
 90        {
 391            if (parameter.ParameterType.IsValueType && Nullable.GetUnderlyingType(parameter.ParameterType) is null)
 92            {
 093                throw new InvalidOperationException(
 094                    $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(selection.Handler)}' route va
 95            }
 96
 397            return null;
 98        }
 99
 23100        var underlyingParameterType = Nullable.GetUnderlyingType(parameter.ParameterType);
 101
 23102        if (underlyingParameterType is not null &&
 23103            underlyingParameterType.IsInstanceOfType(value))
 104        {
 2105            return value;
 106        }
 107
 21108        if (!parameter.ParameterType.IsInstanceOfType(value))
 109        {
 0110            throw new InvalidOperationException(
 0111                $"Telegram handler '{TelegramHandlerDescriptorFormatter.GetDisplayName(selection.Handler)}' route value 
 112        }
 113
 21114        return value;
 115    }
 116}