< Summary

Information
Class: MyNet.Geography.Resources.EmbeddedCountryFlagProvider
Assembly: MyNet.Geography.Resources
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Geography.Resources/EmbeddedCountryFlagProvider.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 1
Uncovered lines: 0
Coverable lines: 1
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
.ctor()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Geography.Resources/EmbeddedCountryFlagProvider.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="EmbeddedCountryFlagProvider.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Globalization;
 10using System.IO;
 11using System.Linq;
 12using System.Reflection;
 13
 14namespace MyNet.Geography.Resources;
 15
 916public sealed class EmbeddedCountryFlagProvider() : EmbeddedCountryFlagProviderBase(typeof(EmbeddedCountryFlagProvider).
 17
 18/// <summary>
 19/// Provides country flags from embedded resources in an assembly. The expected resource naming convention is "fr-32.png
 20/// </summary>
 21public abstract class EmbeddedCountryFlagProviderBase(Assembly assembly) : ICountryFlagProvider
 22{
 23    // key = "fr-32"
 24    private readonly Dictionary<string, string> _index = BuildIndex(assembly);
 25
 26    /// <summary>
 27    /// Builds an index of available flag resources in the assembly. The index maps a key (e.g., "fr-32") to the full re
 28    /// </summary>
 29    /// <param name="assembly">The assembly containing the embedded flag resources.</param>
 30    /// <returns>A dictionary mapping flag keys to resource names.</returns>
 31    private static Dictionary<string, string> BuildIndex(Assembly assembly)
 32        => assembly.GetManifestResourceNames()
 33            .Where(static n => n.EndsWith(".png", StringComparison.OrdinalIgnoreCase))
 34            .ToDictionary(
 35                static n => BuildKey(n),
 36                static n => n,
 37                StringComparer.OrdinalIgnoreCase);
 38
 39    /// <summary>
 40    /// Builds a key for the flag resource based on its name. The key is derived from the file name of the resource, exc
 41    /// </summary>
 42    /// <param name="resourceName">The name of the resource.</param>
 43    /// <returns>The key for the flag resource.</returns>
 44    private static string BuildKey(string resourceName)
 45    {
 46        // Manifest resource names use dots as separators (not directory separators).
 47        // Example: MyNet.Geography.Flags.32x32.fr32.png => key: fr32
 48        var withoutExtension = Path.GetFileNameWithoutExtension(resourceName);
 49        var lastSeparator = withoutExtension.LastIndexOf('.');
 50        var file = lastSeparator >= 0 ? withoutExtension[(lastSeparator + 1)..] : withoutExtension;
 51
 52        // expected: fr32, us64 etc.
 53        return file.ToLower(CultureInfo.CurrentCulture);
 54    }
 55
 56    /// <summary>
 57    /// Opens a stream to the flag image for the specified country and size. The method constructs a key based on the co
 58    /// </summary>
 59    /// <param name="country">The country for which to retrieve the flag.</param>
 60    /// <param name="size">The size of the flag to retrieve.</param>
 61    /// <returns>A stream to the flag image, or null if the flag is not available.</returns>
 62    public Stream? Open(Country country, FlagSize size)
 63    {
 64        var key = $"{country.Alpha2}{(int)size}".ToLower(CultureInfo.CurrentCulture);
 65
 66        return !_index.TryGetValue(key, out var resource) ? null : assembly.GetManifestResourceStream(resource);
 67    }
 68
 69    /// <summary>
 70    /// Gets the flag image as a byte array for the specified country and size. This method internally calls the Open me
 71    /// </summary>
 72    /// <param name="country">The country for which to retrieve the flag.</param>
 73    /// <param name="size">The size of the flag to retrieve.</param>
 74    /// <returns>A byte array containing the flag image, or null if the flag is not available.</returns>
 75    public byte[]? GetBytes(Country country, FlagSize size)
 76    {
 77        using var stream = Open(country, size);
 78        if (stream is null) return null;
 79
 80        using var ms = new MemoryStream();
 81        stream.CopyTo(ms);
 82        return ms.ToArray();
 83    }
 84}
 85

Methods/Properties

.ctor()