This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathJetpackThreatFixStatus.swift
More file actions
79 lines (60 loc) · 2.32 KB
/
Copy pathJetpackThreatFixStatus.swift
File metadata and controls
79 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Foundation
public struct JetpackThreatFixResponse: Decodable {
public let success: Bool
public let threats: [JetpackThreatFixStatus]
public let isFixingThreats: Bool
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
success = try container.decode(Bool.self, forKey: .success)
let statusDict = try container.decode([String: [String: String]].self, forKey: .threats)
var statusArray: [JetpackThreatFixStatus] = []
for (threatId, status) in statusDict {
guard let id = Int(threatId), let statusValue = status["status"] else {
throw ResponseError.decodingFailure
}
let fixStatus = JetpackThreatFixStatus(with: id, status: statusValue)
statusArray.append(fixStatus)
}
isFixingThreats = statusArray.filter { $0.status == .inProgress }.count > 0
threats = statusArray
}
/// Returns true the fixing status is complete, and all threats are no longer in progress
public var finished: Bool {
return inProgress.count <= 0
}
/// Returns all the fixed threats
public var fixed: [JetpackThreatFixStatus] {
return threats.filter { $0.status == .fixed }
}
/// Returns all the in progress threats
public var inProgress: [JetpackThreatFixStatus] {
return threats.filter { $0.status == .inProgress }
}
private enum CodingKeys: String, CodingKey {
case success = "ok", threats
}
enum ResponseError: Error {
case decodingFailure
}
}
public struct JetpackThreatFixStatus {
public enum ThreatFixStatus: String, Decodable, UnknownCaseRepresentable {
case notStarted = "not_started"
case inProgress = "in_progress"
case fixed
case unknown
static let unknownCase: Self = .unknown
}
public let threatId: Int
public let status: ThreatFixStatus
public var threat: JetpackScanThreat?
public init(with threatId: Int, status: String) {
self.threatId = threatId
self.status = ThreatFixStatus(rawValue: status)
}
public init(with threat: JetpackScanThreat, status: ThreatFixStatus = .inProgress) {
self.threat = threat
self.threatId = threat.id
self.status = status
}
}