< Summary - Jellyfin

Information
Class: MediaBrowser.Model.Entities.ProviderIdsExtensions
Assembly: MediaBrowser.Model
File(s): /srv/git/jellyfin/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs
Line coverage
86%
Covered lines: 43
Uncovered lines: 7
Coverable lines: 50
Total lines: 216
Line coverage: 86%
Branch coverage
75%
Covered branches: 18
Total branches: 24
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
HasProviderId(...)100%11100%
HasProviderId(...)100%11100%
TryGetProviderId(...)100%44100%
TryGetProviderId(...)100%11100%
GetProviderId(...)100%11100%
GetProviderId(...)100%11100%
TrySetProviderId(...)90%1010100%
TrySetProviderId(...)100%11100%
SetProviderId(...)66.66%6.97670%
SetProviderId(...)100%11100%
RemoveProviderId(...)0%620%
RemoveProviderId(...)50%22100%

File(s)

/srv/git/jellyfin/MediaBrowser.Model/Entities/ProviderIdsExtensions.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Linq;
 5
 6namespace MediaBrowser.Model.Entities;
 7
 8/// <summary>
 9/// Class ProviderIdsExtensions.
 10/// </summary>
 11public static class ProviderIdsExtensions
 12{
 13    /// <summary>
 14    /// Case insensitive dictionary of <see cref="MetadataProvider"/> string representation.
 15    /// </summary>
 416    private static readonly Dictionary<string, string> _metadataProviderEnumDictionary =
 417        Enum.GetValues<MetadataProvider>()
 418            .ToDictionary(
 419                enumValue => enumValue.ToString(),
 420                enumValue => enumValue.ToString(),
 421                StringComparer.OrdinalIgnoreCase);
 22
 23    /// <summary>
 24    /// Checks if this instance has an id for the given provider.
 25    /// </summary>
 26    /// <param name="instance">The instance.</param>
 27    /// <param name="name">The of the provider name.</param>
 28    /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
 29    public static bool HasProviderId(this IHasProviderIds instance, string name)
 630        => instance.TryGetProviderId(name, out _);
 31
 32    /// <summary>
 33    /// Checks if this instance has an id for the given provider.
 34    /// </summary>
 35    /// <param name="instance">The instance.</param>
 36    /// <param name="provider">The provider.</param>
 37    /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
 38    public static bool HasProviderId(this IHasProviderIds instance, MetadataProvider provider)
 539        => instance.HasProviderId(provider.ToString());
 40
 41    /// <summary>
 42    /// Gets a provider id.
 43    /// </summary>
 44    /// <param name="instance">The instance.</param>
 45    /// <param name="name">The name.</param>
 46    /// <param name="id">The provider id.</param>
 47    /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
 48    public static bool TryGetProviderId(this IHasProviderIds instance, string name, [NotNullWhen(true)] out string? id)
 49    {
 1550        ArgumentNullException.ThrowIfNull(instance);
 51
 1352        if (instance.ProviderIds is null)
 53        {
 354            id = null;
 355            return false;
 56        }
 57
 1058        var foundProviderId = instance.ProviderIds.TryGetValue(name, out id);
 59        // This occurs when searching with Identify (and possibly in other places)
 860        if (string.IsNullOrEmpty(id))
 61        {
 562            id = null;
 563            foundProviderId = false;
 64        }
 65
 866        return foundProviderId;
 67    }
 68
 69    /// <summary>
 70    /// Gets a provider id.
 71    /// </summary>
 72    /// <param name="instance">The instance.</param>
 73    /// <param name="provider">The provider.</param>
 74    /// <param name="id">The provider id.</param>
 75    /// <returns><c>true</c> if a provider id with the given name was found; otherwise <c>false</c>.</returns>
 76    public static bool TryGetProviderId(this IHasProviderIds instance, MetadataProvider provider, [NotNullWhen(true)] ou
 77    {
 478        return instance.TryGetProviderId(provider.ToString(), out id);
 79    }
 80
 81    /// <summary>
 82    /// Gets a provider id.
 83    /// </summary>
 84    /// <param name="instance">The instance.</param>
 85    /// <param name="name">The name.</param>
 86    /// <returns>System.String.</returns>
 87    public static string? GetProviderId(this IHasProviderIds instance, string name)
 88    {
 589        instance.TryGetProviderId(name, out string? id);
 390        return id;
 91    }
 92
 93    /// <summary>
 94    /// Gets a provider id.
 95    /// </summary>
 96    /// <param name="instance">The instance.</param>
 97    /// <param name="provider">The provider.</param>
 98    /// <returns>System.String.</returns>
 99    public static string? GetProviderId(this IHasProviderIds instance, MetadataProvider provider)
 100    {
 4101        return instance.GetProviderId(provider.ToString());
 102    }
 103
 104    /// <summary>
 105    /// Sets a provider id.
 106    /// </summary>
 107    /// <param name="instance">The instance.</param>
 108    /// <param name="name">The name, this should not contain a '=' character.</param>
 109    /// <param name="value">The value.</param>
 110    /// <remarks>Due to how deserialization from the database works the name can not contain '='.</remarks>
 111    /// <returns><c>true</c> if the provider id got set successfully; otherwise, <c>false</c>.</returns>
 112    public static bool TrySetProviderId(this IHasProviderIds instance, string? name, string? value)
 113    {
 27114        ArgumentNullException.ThrowIfNull(instance);
 115
 116        // When name contains a '=' it can't be deserialized from the database
 27117        if (string.IsNullOrWhiteSpace(name)
 27118            || string.IsNullOrWhiteSpace(value)
 27119            || name.Contains('=', StringComparison.Ordinal))
 120        {
 3121            return false;
 122        }
 123
 124        // Ensure it exists
 24125        instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 126
 127        // Match on internal MetadataProvider enum string values before adding arbitrary providers
 24128        if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
 129        {
 23130            instance.ProviderIds[enumValue] = value;
 131        }
 132        else
 133        {
 1134            instance.ProviderIds[name] = value;
 135        }
 136
 24137        return true;
 138    }
 139
 140    /// <summary>
 141    /// Sets a provider id.
 142    /// </summary>
 143    /// <param name="instance">The instance.</param>
 144    /// <param name="provider">The provider.</param>
 145    /// <param name="value">The value.</param>
 146    /// <returns><c>true</c> if the provider id got set successfully; otherwise, <c>false</c>.</returns>
 147    public static bool TrySetProviderId(this IHasProviderIds instance, MetadataProvider provider, string? value)
 18148        => instance.TrySetProviderId(provider.ToString(), value);
 149
 150    /// <summary>
 151    /// Sets a provider id.
 152    /// </summary>
 153    /// <param name="instance">The instance.</param>
 154    /// <param name="name">The name, this should not contain a '=' character.</param>
 155    /// <param name="value">The value.</param>
 156    /// <remarks>Due to how deserialization from the database works the name can not contain '='.</remarks>
 157    public static void SetProviderId(this IHasProviderIds instance, string name, string value)
 158    {
 20159        ArgumentNullException.ThrowIfNull(instance);
 19160        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 19161        ArgumentException.ThrowIfNullOrWhiteSpace(value);
 162
 163        // When name contains a '=' it can't be deserialized from the database
 16164        if (name.Contains('=', StringComparison.Ordinal))
 165        {
 0166            throw new ArgumentException("Provider id name cannot contain '='", nameof(name));
 167        }
 168
 169        // Ensure it exists
 16170        instance.ProviderIds ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 171
 172        // Match on internal MetadataProvider enum string values before adding arbitrary providers
 16173        if (_metadataProviderEnumDictionary.TryGetValue(name, out var enumValue))
 174        {
 16175            instance.ProviderIds[enumValue] = value;
 176        }
 177        else
 178        {
 0179            instance.ProviderIds[name] = value;
 180        }
 0181    }
 182
 183    /// <summary>
 184    /// Sets a provider id.
 185    /// </summary>
 186    /// <param name="instance">The instance.</param>
 187    /// <param name="provider">The provider.</param>
 188    /// <param name="value">The value.</param>
 189    public static void SetProviderId(this IHasProviderIds instance, MetadataProvider provider, string value)
 11190        => instance.SetProviderId(provider.ToString(), value);
 191
 192    /// <summary>
 193    /// Removes a provider id.
 194    /// </summary>
 195    /// <param name="instance">The instance.</param>
 196    /// <param name="name">The name.</param>
 197    public static void RemoveProviderId(this IHasProviderIds instance, string name)
 198    {
 0199        ArgumentNullException.ThrowIfNull(instance);
 0200        ArgumentException.ThrowIfNullOrEmpty(name);
 201
 0202        instance.ProviderIds?.Remove(name);
 0203    }
 204
 205    /// <summary>
 206    /// Removes a provider id.
 207    /// </summary>
 208    /// <param name="instance">The instance.</param>
 209    /// <param name="provider">The provider.</param>
 210    public static void RemoveProviderId(this IHasProviderIds instance, MetadataProvider provider)
 211    {
 1212        ArgumentNullException.ThrowIfNull(instance);
 213
 1214        instance.ProviderIds?.Remove(provider.ToString());
 1215    }
 216}