-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMailbotFactory.cs
More file actions
66 lines (56 loc) · 2.37 KB
/
MailbotFactory.cs
File metadata and controls
66 lines (56 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MailbotFactory.cs" company="Lord Design">
// © 2022 Lord Design
// </copyright>
// <license type="GPL-3.0">
// You may use freely and commercially without modification; if you make changes, please share back to the
// community.
// </license>
// <author>Aaron Lord</author>
// --------------------------------------------------------------------------------------------------------------------
using System.Runtime.CompilerServices;
using Devlord.Utilities.Exceptions;
using Microsoft.Extensions.Options;
[assembly: InternalsVisibleTo("Devlord.Utilities.Tests")]
namespace Devlord.Utilities.Mail
{
public class MailbotFactory : IMailbotFactory
{
private static readonly object DictionaryLock = new object();
private static readonly Dictionary<string, Mailbot> Instances = new Dictionary<string, Mailbot>();
private readonly DevlordOptions _options;
public MailbotFactory(IOptionsMonitor<DevlordOptions> options)
{
// Note: This does not reload settings when they change because we're using a singleton.
_options = options.CurrentValue;
}
/// <summary>
/// Gets the instance.
/// </summary>
public Mailbot GetMailbot(string name)
{
lock (DictionaryLock)
{
if (Instances.ContainsKey(name))
{
return Instances[name];
}
var thisOptions = _options.MailSettings.FirstOrDefault(n => n.Name == name);
if (thisOptions == null)
{
throw new DevlordConfigurationException($"Missing mail options for name {name}");
}
var instance = new Mailbot
{
SmtpServer = thisOptions.SmtpServer,
SmtpPort = thisOptions.SmtpPort,
SmtpLogin = thisOptions.SmtpLogin,
SmtpPassword = thisOptions.SmtpPassword,
Throttles = new Throttles(thisOptions.MaxPerMinute, thisOptions.MaxPerHour, thisOptions.MaxPerDay)
};
Instances.Add(name, instance);
return instance;
}
}
}
}