<commit_before>﻿using CK.Glouton.AlertSender.Sender;
using CK.Glouton.Model.Server.Sender;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace CK.Glouton.AlertSender
{
    public class AlertSenderManager
    {
        private readonly IList<IAlertSender> _senders;

        public AlertSenderManager()
        {
            _senders = new List<IAlertSender>();
        }

        public IAlertSender Parse( IAlertSenderConfiguration configuration )
        {
            foreach( var existingSender in _senders )
                if( existingSender.Match( configuration ) )
                    return existingSender;

            IAlertSender newSender;
            switch( configuration.SenderType )
            {
                case "Mail":
                    if( !( configuration is MailSenderConfiguration mailSenderConfiguration ) )
                        throw new ArgumentException( nameof( configuration.SenderType ) );
                    newSender = CreateSender( mailSenderConfiguration );
                    break;

                case "Http":
                    if( !( configuration is HttpSenderConfiguration httpSenderConfiguration ) )
                        throw new ArgumentException( nameof( configuration.SenderType ) );
                    newSender = CreateSender( httpSenderConfiguration );
                    break;

                default:
                    throw new ArgumentException( nameof( configuration ) );
            }
            _senders.Add( newSender );
            return newSender;

        }

        public static Func<IAlertSenderConfiguration, IAlertSender> CreateSender = configuration =>
        {
            var name = configuration.GetType().GetTypeInfo().FullName;
            if( !name.EndsWith( "Configuration" ) )
                throw new Exception( $"Configuration sender type name must end with 'Configuration': {name}." );
            name = configuration.GetType().AssemblyQualifiedName.Replace( "Configuration,", "," );
            var type = Type.GetType( name, true );
            return (IAlertSender)Activator.CreateInstance( type, configuration );
        };
    }
}<commit_msg>We don't need to assert configuration here.<commit_after>﻿using CK.Glouton.Model.Server.Sender;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace CK.Glouton.AlertSender
{
    public class AlertSenderManager
    {
        private readonly IList<IAlertSender> _senders;

        public AlertSenderManager()
        {
            _senders = new List<IAlertSender>();
        }

        public IAlertSender Parse( IAlertSenderConfiguration configuration )
        {
            foreach( var existingSender in _senders )
                if( existingSender.Match( configuration ) )
                    return existingSender;

            var newSender = CreateSender( configuration );
            _senders.Add( newSender );
            return newSender;

        }

        public static Func<IAlertSenderConfiguration, IAlertSender> CreateSender = configuration =>
        {
            var name = configuration.GetType().GetTypeInfo().FullName;
            if( !name.EndsWith( "Configuration" ) )
                throw new Exception( $"Configuration sender type name must end with 'Configuration': {name}." );
            name = configuration.GetType().AssemblyQualifiedName.Replace( "Configuration,", "," );
            var type = Type.GetType( name, true );
            return (IAlertSender)Activator.CreateInstance( type, configuration );
        };
    }
}
{'fraction_non_alphanumeric': 0.08282036933407946, 'fraction_numerical': 0.0, 'mean_word_length': 2.1168265039232783, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5368', 'n_tokens_mistral': 840, 'n_tokens_neox': 804, 'n_words': 208}