2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// ProfileMapper.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/18/22.
|
2023-03-17 15:56:19 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreData
|
2023-04-04 07:50:45 +00:00
|
|
|
import Foundation
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
2023-05-24 16:19:47 +00:00
|
|
|
import PassepartoutVPN
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
struct ProfileMapper: DTOMapper, ModelMapper {
|
|
|
|
private let context: NSManagedObjectContext
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(_ context: NSManagedObjectContext) {
|
|
|
|
self.context = context
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func toDTO(_ ws: Profile) throws -> CDProfile {
|
|
|
|
let profile = ProfileHeaderMapper(context).toDTO(ws)
|
|
|
|
do {
|
2023-12-17 22:31:08 +00:00
|
|
|
profile.encryptedJSON = try JSONEncoder().encode(ws)
|
2022-04-12 13:09:14 +00:00
|
|
|
} catch {
|
|
|
|
assertionFailure("Unable to encode profile: \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
|
|
|
|
static func toModel(_ dto: CDProfile) throws -> Profile? {
|
2023-12-17 22:31:08 +00:00
|
|
|
guard let json = dto.encryptedJSON ?? dto.json else {
|
2023-05-27 10:14:04 +00:00
|
|
|
Utils.assertCoreDataDecodingFailed()
|
2022-04-12 13:09:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
return try JSONDecoder().decode(Profile.self, from: json)
|
|
|
|
} catch {
|
|
|
|
assertionFailure("Unable to decode profile: \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ProfileHeaderMapper: DTOMapper, ModelMapper {
|
|
|
|
private let context: NSManagedObjectContext
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
init(_ context: NSManagedObjectContext) {
|
|
|
|
self.context = context
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
func toDTO(_ ws: Profile) -> CDProfile {
|
|
|
|
let profile = CDProfile(context: context)
|
|
|
|
profile.uuid = ws.header.id
|
|
|
|
profile.name = ws.header.name
|
|
|
|
profile.providerName = ws.header.providerName
|
|
|
|
profile.lastUpdate = Date()
|
|
|
|
return profile
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
static func toModel(_ dto: CDProfile) -> Profile.Header? {
|
|
|
|
guard let uuid = dto.uuid,
|
|
|
|
let name = dto.name else {
|
|
|
|
|
2023-05-27 10:14:04 +00:00
|
|
|
Utils.assertCoreDataDecodingFailed()
|
2022-04-12 13:09:14 +00:00
|
|
|
return nil
|
2023-03-17 20:55:47 +00:00
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
return Profile.Header(
|
|
|
|
uuid: uuid,
|
|
|
|
name: name,
|
|
|
|
providerName: dto.providerName,
|
|
|
|
lastUpdate: dto.lastUpdate
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|