<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Ghpr.Core.Common;
using Ghpr.Core.Enums;
using Ghpr.Core.Helpers;
using Ghpr.Core.Interfaces;
using Ghpr.Core.Processors;
using Ghpr.Core.Providers;
using Ghpr.Core.Utils;

namespace Ghpr.Core.Factories
{
    public static class ReporterFactory
    {
        public static IReporter Build(ITestDataProvider testDataProvider)
        {
            return InitializeReporter(ReporterSettingsProvider.Load(Paths.Files.CoreSettings), testDataProvider);
        }

        public static IReporter Build(ReporterSettings settings, ITestDataProvider testDataProvider)
        {
            return InitializeReporter(settings, testDataProvider);
        }

        public static IReporter Build(TestingFramework framework, ITestDataProvider testDataProvider)
        {
            return InitializeReporter(ReporterSettingsProvider.Load(framework), testDataProvider);
        }

        private static T CreateInstanceFromFile<T>(string fileName, T defaultImplementation = null) where T : class
        {
            if (fileName.Equals("") && defaultImplementation != null)
            {
                return defaultImplementation;
            }
            var uri = new Uri(typeof(ReporterFactory).Assembly.CodeBase);
            var instanceAssemblyFullPath = Path.Combine(Path.GetDirectoryName(uri.LocalPath) ?? "", fileName);
            var instanceAssembly = Assembly.LoadFrom(instanceAssemblyFullPath);
            var implementationType = instanceAssembly.GetTypes()
                .FirstOrDefault(t => typeof(T).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
            if (implementationType == null)
            {
                throw new NullReferenceException($"Can't find implementation of {nameof(T)} in {fileName} file. " +
                                                 "Please fix your .json settings file.");
            }
            var instance = Activator.CreateInstance(implementationType) as T;
            if (instance == null)
            {
                throw new NullReferenceException($"Can't find create instance of type {nameof(implementationType)} from {fileName} file. " +
                                                 "Please fix your .json settings file.");
            }
            return instance;
        }

        private static IReporter InitializeReporter(ReporterSettings settings, ITestDataProvider testDataProvider)
        {
            if (settings.OutputPath == null)
            {
                throw new ArgumentNullException(nameof(settings.OutputPath),
                    "Reporter Output path must be specified. Please fix your .json settings file.");
            }

            var logger = CreateInstanceFromFile<ILogger>(settings.LoggerFile, new EmptyLogger());
            logger.SetUp(settings);

            var dataWriterService = CreateInstanceFromFile<IDataWriterService>(settings.DataServiceFile);
            dataWriterService.Initialize(settings, logger);

            var dataReaderService = CreateInstanceFromFile<IDataReaderService>(settings.DataServiceFile);
            dataReaderService.Initialize(settings, logger);

            var reporter = new Reporter
            {
                Action = new ActionHelper(logger),
                Logger = logger,
                TestDataProvider = testDataProvider,
                ReporterSettings = settings,
                ReportSettings = new ReportSettingsDto(settings.RunsToDisplay, settings.TestsToDisplay),
                DataWriterService = dataWriterService,
                DataReaderService = dataReaderService,
                RunRepository = new RunDtoRepository(),
                TestRunsRepository = new TestRunsRepository(),
                TestRunProcessor = new TestRunDtoProcessor(),
                TestRunStarted = false
            };
            return reporter;
        }
    }
}<commit_msg>decorator usage<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Ghpr.Core.Common;
using Ghpr.Core.Enums;
using Ghpr.Core.Helpers;
using Ghpr.Core.Interfaces;
using Ghpr.Core.Processors;
using Ghpr.Core.Providers;
using Ghpr.Core.Services;
using Ghpr.Core.Utils;

namespace Ghpr.Core.Factories
{
    public static class ReporterFactory
    {
        public static IReporter Build(ITestDataProvider testDataProvider)
        {
            return InitializeReporter(ReporterSettingsProvider.Load(Paths.Files.CoreSettings), testDataProvider);
        }

        public static IReporter Build(ReporterSettings settings, ITestDataProvider testDataProvider)
        {
            return InitializeReporter(settings, testDataProvider);
        }

        public static IReporter Build(TestingFramework framework, ITestDataProvider testDataProvider)
        {
            return InitializeReporter(ReporterSettingsProvider.Load(framework), testDataProvider);
        }

        private static T CreateInstanceFromFile<T>(string fileName, T defaultImplementation = null) where T : class
        {
            if (fileName.Equals("") && defaultImplementation != null)
            {
                return defaultImplementation;
            }
            var uri = new Uri(typeof(ReporterFactory).Assembly.CodeBase);
            var instanceAssemblyFullPath = Path.Combine(Path.GetDirectoryName(uri.LocalPath) ?? "", fileName);
            var instanceAssembly = Assembly.LoadFrom(instanceAssemblyFullPath);
            var implementationType = instanceAssembly.GetTypes()
                .FirstOrDefault(t => typeof(T).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
            if (implementationType == null)
            {
                throw new NullReferenceException($"Can't find implementation of {nameof(T)} in {fileName} file. " +
                                                 "Please fix your .json settings file.");
            }
            var instance = Activator.CreateInstance(implementationType) as T;
            if (instance == null)
            {
                throw new NullReferenceException($"Can't find create instance of type {nameof(implementationType)} from {fileName} file. " +
                                                 "Please fix your .json settings file.");
            }
            return instance;
        }

        private static IReporter InitializeReporter(ReporterSettings settings, ITestDataProvider testDataProvider)
        {
            if (settings.OutputPath == null)
            {
                throw new ArgumentNullException(nameof(settings.OutputPath),
                    "Reporter Output path must be specified. Please fix your .json settings file.");
            }

            var logger = CreateInstanceFromFile<ILogger>(settings.LoggerFile, new EmptyLogger());
            logger.SetUp(settings);

            var dataWriterService = CreateInstanceFromFile<IDataWriterService>(settings.DataServiceFile);
            dataWriterService.Initialize(settings, logger);

            var dataReaderService = CreateInstanceFromFile<IDataReaderService>(settings.DataServiceFile);
            dataReaderService.Initialize(settings, logger);

            var reporter = new Reporter
            {
                Action = new ActionHelper(logger),
                Logger = logger,
                TestDataProvider = testDataProvider,
                ReporterSettings = settings,
                ReportSettings = new ReportSettingsDto(settings.RunsToDisplay, settings.TestsToDisplay),
                DataWriterService = dataWriterService,
                DataReaderService = new DataReaderService(dataReaderService),
                RunRepository = new RunDtoRepository(),
                TestRunsRepository = new TestRunsRepository(),
                TestRunProcessor = new TestRunDtoProcessor(),
                TestRunStarted = false
            };
            return reporter;
        }
    }
}
{'fraction_non_alphanumeric': 0.07657487740475293, 'fraction_numerical': 0.0, 'mean_word_length': 2.26652977412731, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3614542', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1801, 'n_words': 430}