< Summary

Information
Class: Services.EmailRoutingServices.EmailImapSettingsInput
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailRoutingServices/ImapSettingsStore.cs
Line coverage
100%
Covered lines: 7
Uncovered lines: 0
Coverable lines: 7
Total lines: 85
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Host()100%11100%
get_Port()100%11100%
get_UserName()100%11100%
get_Password()100%11100%
get_UseSsl()100%11100%
get_Folder()100%11100%
get_FetchLimit()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/EmailRoutingServices/ImapSettingsStore.cs

#LineLine coverage
 1using Microsoft.Extensions.Configuration;
 2using Microsoft.Extensions.Hosting;
 3using Microsoft.Extensions.Logging;
 4using Microsoft.Extensions.Options;
 5using Newtonsoft.Json;
 6using Newtonsoft.Json.Linq;
 7
 8namespace Services.EmailRoutingServices;
 9
 10public interface IImapSettingsStore
 11{
 12    ImapSettings GetCurrent();
 13    ImapSettings MergeWithCurrent(EmailImapSettingsInput input);
 14    Task SaveAsync(EmailImapSettingsInput input, CancellationToken cancellationToken = default);
 15}
 16
 17public class EmailImapSettingsInput
 18{
 1519    public string Host { get; set; } = string.Empty;
 1520    public int Port { get; set; } = 993;
 1521    public string UserName { get; set; } = string.Empty;
 1322    public string? Password { get; set; }
 1523    public bool UseSsl { get; set; } = true;
 1624    public string Folder { get; set; } = "INBOX";
 1525    public int FetchLimit { get; set; } = 20;
 26}
 27
 28public class FileImapSettingsStore : IImapSettingsStore
 29{
 30    private readonly IHostEnvironment _environment;
 31    private readonly IConfiguration _configuration;
 32    private readonly IOptionsMonitor<ImapSettings> _settingsMonitor;
 33    private readonly ILogger<FileImapSettingsStore> _logger;
 34
 35    public FileImapSettingsStore(
 36        IHostEnvironment environment,
 37        IConfiguration configuration,
 38        IOptionsMonitor<ImapSettings> settingsMonitor,
 39        ILogger<FileImapSettingsStore> logger)
 40    {
 41        _environment = environment;
 42        _configuration = configuration;
 43        _settingsMonitor = settingsMonitor;
 44        _logger = logger;
 45    }
 46
 47    public ImapSettings GetCurrent() => _settingsMonitor.CurrentValue;
 48
 49    public ImapSettings MergeWithCurrent(EmailImapSettingsInput input)
 50    {
 51        var current = GetCurrent();
 52        return new ImapSettings
 53        {
 54            Host = input.Host.Trim(),
 55            Port = input.Port,
 56            UserName = input.UserName.Trim(),
 57            Password = string.IsNullOrWhiteSpace(input.Password) ? current.Password : input.Password,
 58            UseSsl = input.UseSsl,
 59            Folder = string.IsNullOrWhiteSpace(input.Folder) ? "INBOX" : input.Folder.Trim(),
 60            FetchLimit = input.FetchLimit,
 61            FetchScanLimit = current.FetchScanLimit,
 62            FetchWindowDays = current.FetchWindowDays,
 63            FetchAfterUtc = current.FetchAfterUtc
 64        };
 65    }
 66
 67    public async Task SaveAsync(EmailImapSettingsInput input, CancellationToken cancellationToken = default)
 68    {
 69        var merged = MergeWithCurrent(input);
 70        var configPath = AppRuntimeSettingsPaths.ResolvePath(_environment.ContentRootPath, _configuration);
 71        await AppRuntimeSettingsWriter.SaveAsync(
 72            _environment.ContentRootPath,
 73            _configuration,
 74            root =>
 75            {
 76                root["ImapSettings"] = JObject.FromObject(merged, JsonSerializer.Create(new JsonSerializerSettings
 77                {
 78                    NullValueHandling = NullValueHandling.Ignore
 79                }));
 80            },
 81            cancellationToken);
 82
 83        _logger.LogInformation("ImapSettings saved to {ConfigPath} for mailbox {UserName}", configPath, merged.UserName)
 84    }
 85}