Skip to content

Commit 3948833

Browse files
authored
Refactor License Check (#702)
1 parent 9adac5f commit 3948833

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/Nager.Date/HolidaySystem.cs

+20-14
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
using Nager.Date.HolidayProviders;
33
using Nager.Date.Models;
44
using Nager.Date.ReligiousProviders;
5-
using Nager.LicenseSystem;
65
using System;
76
using System.Collections.Generic;
87
using System.Linq;
9-
using System.Runtime.CompilerServices;
108

119
namespace Nager.Date
1210
{
@@ -141,7 +139,7 @@ public static class HolidaySystem
141139
{ CountryCode.ZW, new Lazy<IHolidayProvider>(() => new ZimbabweHolidayProvider(_catholicProvider))}
142140
};
143141

144-
private static bool? _licenseValid;
142+
private static LicenseCheckStatus _licenseCheckStatus = LicenseCheckStatus.NotChecked;
145143

146144
/// <summary>
147145
/// License Key
@@ -155,24 +153,24 @@ private static void CheckLicense(string? licenseKey)
155153
{
156154
if (string.IsNullOrEmpty(licenseKey))
157155
{
158-
_licenseValid = false;
159-
throw new LicenseKeyException("No LicenseKey");
156+
_licenseCheckStatus = LicenseCheckStatus.NotConfigured;
157+
return;
160158
}
161159

162160
var licenseInfo = LicenseHelper.CheckLicenseKey(licenseKey);
163161
if (licenseInfo is null)
164162
{
165-
_licenseValid = false;
166-
throw new LicenseKeyException("Invalid LicenseKey");
163+
_licenseCheckStatus = LicenseCheckStatus.Invalid;
164+
return;
167165
}
168166

169167
if (licenseInfo.ValidUntil < DateTime.Today)
170168
{
171-
_licenseValid = false;
172-
throw new LicenseKeyException("Expried LicenseKey");
169+
_licenseCheckStatus = LicenseCheckStatus.Expired;
170+
return;
173171
}
174172

175-
_licenseValid = true;
173+
_licenseCheckStatus = LicenseCheckStatus.Valid;
176174
}
177175

178176
/// <summary>
@@ -211,15 +209,23 @@ public static IHolidayProvider GetHolidayProvider(CountryCode countryCode)
211209
/// <returns></returns>
212210
public static bool TryGetHolidayProvider(CountryCode countryCode, out IHolidayProvider holidayProvider)
213211
{
214-
if (_licenseValid is null)
212+
if (_licenseCheckStatus == LicenseCheckStatus.NotChecked)
215213
{
216214
CheckLicense(LicenseKey);
217215
}
218216

219-
if (_licenseValid is not null && !_licenseValid.Value)
217+
switch (_licenseCheckStatus)
220218
{
221-
holidayProvider = NoHolidaysHolidayProvider.Instance;
222-
return false;
219+
case LicenseCheckStatus.Valid:
220+
break;
221+
case LicenseCheckStatus.NotConfigured:
222+
throw new LicenseKeyException("No LicenseKey");
223+
case LicenseCheckStatus.Invalid:
224+
throw new LicenseKeyException("Invalid LicenseKey");
225+
case LicenseCheckStatus.Expired:
226+
throw new LicenseKeyException("Expried LicenseKey");
227+
default:
228+
throw new LicenseKeyException("Unknown LicenseKey Check Status");
223229
}
224230

225231
if (_holidaysProviders.TryGetValue(countryCode, out var provider))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Nager.Date.Models
2+
{
3+
/// <summary>
4+
/// License Check Status
5+
/// </summary>
6+
internal enum LicenseCheckStatus
7+
{
8+
/// <summary>
9+
/// License key is missing or not configured
10+
/// </summary>
11+
NotConfigured,
12+
13+
/// <summary>
14+
/// License key has not been checked
15+
/// </summary>
16+
NotChecked,
17+
18+
/// <summary>
19+
/// License key is invalid
20+
/// </summary>
21+
Invalid,
22+
23+
/// <summary>
24+
/// License key has expired
25+
/// </summary>
26+
Expired,
27+
28+
/// <summary>
29+
/// License key is valid
30+
/// </summary>
31+
Valid
32+
}
33+
}

0 commit comments

Comments
 (0)