< Summary - Syki

Information
Class: Syki.Back.Configs.WebhookConfigs
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Configs/WebhookConfigs.cs
Tag: 97_27801654829
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 57
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
AddWebhookEventConfigs(...)0%210140%
TryGetEventType(...)100%210%

File(s)

/home/runner/work/syki/syki/Back/Configs/WebhookConfigs.cs

#LineLine coverage
 1using Syki.Back.Webhooks;
 2
 3namespace Syki.Back.Configs;
 4
 5public static class WebhookConfigs
 6{
 07    private static readonly Dictionary<string, Type> _eventTypes = [];
 8
 9    public static void AddWebhookEventConfigs(this WebApplicationBuilder builder)
 10    {
 011        var assemblies = AppDomain.CurrentDomain.GetAssemblies()
 012            .Where(s => s.FullName!.StartsWith("Back"))
 013            .ToList();
 14
 015        var eventTypes = assemblies.SelectMany(s => s.GetTypes())
 016            .Where(t => t.IsAssignableTo(typeof(IWebhookEvent)) && !t.IsInterface)
 017            .ToList();
 18
 019        foreach (var type in eventTypes)
 20        {
 021            var attr = type.GetCustomAttributes(typeof(WebhookEventTypeAttribute), false)
 022                .Cast<WebhookEventTypeAttribute>()
 023                .FirstOrDefault();
 24
 025            if (attr == null) throw new InvalidOperationException($"IWebhookEvent '{type.Name}' is missing [WebhookEvent
 26
 027            _eventTypes[attr.Type] = type;
 28        }
 29
 030        var handlerTypes = assemblies.SelectMany(s => s.GetTypes())
 031            .Where(t => !t.IsInterface && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == 
 032            .ToList();
 33
 034        var handledEventTypes = new HashSet<Type>();
 35
 036        foreach (var type in handlerTypes)
 37        {
 038            var handlerInterface = type.GetInterfaces()
 039                .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IWebhookEventHandler<>));
 40
 041            handledEventTypes.Add(handlerInterface.GetGenericArguments()[0]);
 042            builder.Services.AddTransient(handlerInterface, type);
 43        }
 44
 045        var eventsWithoutHandler = eventTypes.Except(handledEventTypes).ToList();
 046        if (eventsWithoutHandler.Count > 0)
 47        {
 048            var names = string.Join(", ", eventsWithoutHandler.Select(t => t.Name));
 049            throw new InvalidOperationException($"Webhook events without a registered IWebhookEventHandler<T>: {names}")
 50        }
 051    }
 52
 53    public static bool TryGetEventType(string type, out Type eventType)
 54    {
 055        return _eventTypes.TryGetValue(type, out eventType!);
 56    }
 57}