< Summary

Class:ApplicationCore.WebConfiguration
Assembly:ApplicationCore
File(s):C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Configuration\WebConfiguration.cs
Covered lines:8
Uncovered lines:0
Coverable lines:8
Total lines:99
Line coverage:100% (8 of 8)
Covered branches:0
Total branches:0

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)10100%100%

File(s)

C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Configuration\WebConfiguration.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Globalization;
 4using System.Net.Mail;
 5using Microsoft.Extensions.Configuration;
 6
 7namespace ApplicationCore
 8{
 9    public interface IWebConfiguration
 10    {
 11        CommentConfig Comment { get; }
 12        GitHubConfig GitHub { get; }
 13        TextAnalyticsConfig TextAnalytics { get; }
 14    }
 15
 16    public class WebConfiguration : IWebConfiguration
 17    {
 618        public CommentConfig Comment => _configProvider.GetConfig<CommentConfig>(_config);
 19
 620        public GitHubConfig GitHub => _configProvider.GetConfig<GitHubConfig>(_config);
 21
 522        public TextAnalyticsConfig TextAnalytics => _configProvider.GetConfig<TextAnalyticsConfig>(_config);
 23
 24        private readonly IConfigurationRoot _config;
 25        private readonly IConfigProvider _configProvider;
 26
 927        public WebConfiguration(IConfigurationRoot config, IConfigProvider provider)
 928        {
 929            _config = config;
 930            _configProvider = provider;
 931        }
 32    }
 33
 34    public class CommentConfig
 35    {
 36        public Uri WebsiteUrl { get; set; } = null!;
 37
 38        public MailAddress FallbackCommitEmailAddress => new MailAddress(FallbackCommitEmail);
 39
 40        /// <summary>
 41        /// <see cref="MailAddress"/> is non-serializable. This property is used as a proxy when
 42        /// <see cref="BinderOptions.BindNonPublicProperties"/> is set to <c>true</c> and when binding the section to
 43        /// the actual config instance.
 44        /// </summary>
 45        private string FallbackCommitEmail { get; set; } = "";
 46
 47        public CommentConfig() { }
 48
 49        public CommentConfig(string website, string fallbackCommitEmailAddress)
 50        {
 51            WebsiteUrl = GetValidUri(website);
 52            FallbackCommitEmail = GetValidEmail(fallbackCommitEmailAddress);
 53        }
 54
 55        private Uri GetValidUri(string website)
 56        {
 57            if (!Uri.TryCreate(website, UriKind.Absolute, out var uri)
 58                && (uri == null || (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)))
 59            {
 60                throw new ArgumentException(
 61                    string.Format(
 62                        CultureInfo.InvariantCulture,
 63                        CommentResources.WebsiteInvalidUriErrorMessage,
 64                        website),
 65                    nameof(website));
 66            }
 67
 68            return uri;
 69        }
 70
 71        private string GetValidEmail(string email)
 72        {
 73            if(!new EmailAddressAttribute().IsValid(email))
 74            {
 75                throw new ArgumentException(
 76                    string.Format(
 77                        CultureInfo.InvariantCulture,
 78                        CommentResources.FallbackCommitEmailInvalidErrorMessage,
 79                        email),
 80                    nameof(email));
 81            }
 82
 83            return email;
 84        }
 85    }
 86
 87    public class GitHubConfig
 88    {
 89        public string Token { get; set; } = "";
 90        public string PullRequestRepository { get; set; } = "";
 91    }
 92
 93    public class TextAnalyticsConfig
 94    {
 95        public string SubscriptionKey { get; set; } = "";
 96        public string Region { get; set; } = "";
 97        public string Language { get; set; } = "";
 98    }
 99}