/* * Copyright (c) 2023 Proton AG * * This file is part of ProtonVPN. * * ProtonVPN is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ProtonVPN is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ProtonVPN. If not, see . */ using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.Cities; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.Countries; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.FreeServers; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.Gateways; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.GatewayServers; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.Servers; using ProtonVPN.Client.Logic.Connection.Contracts.Models.Intents.Locations.States; using ProtonVPN.Client.Logic.Connection.Contracts.SerializableEntities.Intents; using ProtonVPN.EntityMapping.Contracts; namespace ProtonVPN.Client.Logic.Connection.EntityMapping; public class LocationIntentMapper : IMapper { private readonly IEntityMapper _entityMapper; private const string LEGACY_SERVER_LOCATION_INTENT = "ServerLocationIntent"; private const string LEGACY_CITY_LOCATION_INTENT = "CityLocationIntent"; private const string LEGACY_STATE_LOCATION_INTENT = "StateLocationIntent"; private const string LEGACY_COUNTRY_LOCATION_INTENT = "CountryLocationIntent"; private const string LEGACY_GATEWAY_SERVER_LOCATION_INTENT = "GatewayServerLocationIntent"; private const string LEGACY_GATEWAY_LOCATION_INTENT = "GatewayLocationIntent"; public LocationIntentMapper(IEntityMapper entityMapper) { _entityMapper = entityMapper; } public SerializableLocationIntent Map(ILocationIntent leftEntity) { return leftEntity is null ? null : leftEntity switch { SingleServerLocationIntent intent => _entityMapper.Map(intent), MultiServerLocationIntent intent => _entityMapper.Map(intent), SingleCityLocationIntent intent => _entityMapper.Map(intent), MultiCityLocationIntent intent => _entityMapper.Map(intent), SingleStateLocationIntent intent => _entityMapper.Map(intent), MultiStateLocationIntent intent => _entityMapper.Map(intent), SingleCountryLocationIntent intent => _entityMapper.Map(intent), MultiCountryLocationIntent intent => _entityMapper.Map(intent), SingleGatewayServerLocationIntent intent => _entityMapper.Map(intent), MultiGatewayServerLocationIntent intent => _entityMapper.Map(intent), SingleGatewayLocationIntent intent => _entityMapper.Map(intent), MultiGatewayLocationIntent intent => _entityMapper.Map(intent), FreeServerLocationIntent intent => _entityMapper.Map(intent), _ => throw new NotImplementedException($"No mapping is implemented for {leftEntity.GetType().FullName}"), }; } public ILocationIntent Map(SerializableLocationIntent rightEntity) { return rightEntity is null ? null : rightEntity.TypeName switch { // Legacy types mapping LEGACY_SERVER_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.Id) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), LEGACY_CITY_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.City) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), LEGACY_STATE_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.State) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), LEGACY_COUNTRY_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.CountryCode) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), LEGACY_GATEWAY_SERVER_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.Id) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), LEGACY_GATEWAY_LOCATION_INTENT => string.IsNullOrEmpty(rightEntity.GatewayName) ? _entityMapper.Map(rightEntity) : _entityMapper.Map(rightEntity), nameof(SingleServerLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiServerLocationIntent) => _entityMapper.Map(rightEntity), nameof(SingleCityLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiCityLocationIntent) => _entityMapper.Map(rightEntity), nameof(SingleStateLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiStateLocationIntent) => _entityMapper.Map(rightEntity), nameof(SingleCountryLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiCountryLocationIntent) => _entityMapper.Map(rightEntity), nameof(SingleGatewayServerLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiGatewayServerLocationIntent) => _entityMapper.Map(rightEntity), nameof(SingleGatewayLocationIntent) => _entityMapper.Map(rightEntity), nameof(MultiGatewayLocationIntent) => _entityMapper.Map(rightEntity), nameof(FreeServerLocationIntent) => _entityMapper.Map(rightEntity), _ => throw new NotImplementedException($"No mapping is implemented for {rightEntity.TypeName}"), }; } }