Skip to content

Commit fd8c6de

Browse files
committed
display receipt: make methods non static, so that they can be testable
1 parent a404332 commit fd8c6de

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

Sources/Swift/Display Receipt/DisplayReceiptCacheHelper.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import Foundation
99

1010
internal struct DisplayReceiptCacheHelper {
1111

12-
private static let coordinator = NSFileCoordinator(filePresenter: nil)
12+
private let coordinator = NSFileCoordinator(filePresenter: nil)
1313

14-
static func sharedGroupId() throws -> String {
14+
func sharedGroupId() throws -> String {
1515

1616
let groupIdOverride = Bundle.main.object(forInfoDictionaryKey: "BATCH_APP_GROUP_ID")
1717
if let groupId = groupIdOverride as? String, !groupId.isEmpty {
@@ -26,7 +26,7 @@ internal struct DisplayReceiptCacheHelper {
2626
throw DisplayReceiptHelperError.appGroupError
2727
}
2828

29-
static func sharedDirectory() throws -> URL {
29+
func sharedDirectory() throws -> URL {
3030
do {
3131
guard let sharedDir = FileManager
3232
.default
@@ -41,7 +41,7 @@ internal struct DisplayReceiptCacheHelper {
4141
}
4242
}
4343

44-
static func sharedDefaults() throws -> UserDefaults {
44+
func sharedDefaults() throws -> UserDefaults {
4545
let groupId = try self.sharedGroupId()
4646
guard let defaults = UserDefaults.init(suiteName: groupId)
4747
else { throw DisplayReceiptHelperError.appGroupError }
@@ -50,11 +50,11 @@ internal struct DisplayReceiptCacheHelper {
5050

5151
// MARK: Methods updating cache files
5252

53-
static func newFilename() -> String {
53+
func newFilename() -> String {
5454
return String(format: Consts.receiptCacheFileFormat, UUID().uuidString)
5555
}
5656

57-
static func write(toFile file: URL, _ data: Data) throws {
57+
func write(toFile file: URL, _ data: Data) throws {
5858
var error: NSError?
5959
var writeError: Error?
6060
coordinator.coordinate(writingItemAt: file, options: .forReplacing, error: &error) { url in
@@ -70,7 +70,7 @@ internal struct DisplayReceiptCacheHelper {
7070
}
7171
}
7272

73-
static func write(_ data: Data) throws {
73+
func write(_ data: Data) throws {
7474
do {
7575
let cacheDir = try sharedDirectory()
7676
let cacheFile = cacheDir.appendingPathComponent(newFilename())
@@ -81,7 +81,7 @@ internal struct DisplayReceiptCacheHelper {
8181
}
8282
}
8383

84-
static func read(fromFile file: URL) throws -> Data {
84+
func read(fromFile file: URL) throws -> Data {
8585
var error: NSError?
8686
var data: Data?
8787
coordinator.coordinate(readingItemAt: file, options: .withoutChanges, error: &error) { url in
@@ -98,7 +98,7 @@ internal struct DisplayReceiptCacheHelper {
9898
throw DisplayReceiptHelperError.readCacheError(underlyingError: error ?? NSError())
9999
}
100100

101-
static func delete(_ file: URL) -> Error? {
101+
func delete(_ file: URL) -> Error? {
102102
var error: NSError?
103103
var deleteError: Error?
104104
coordinator.coordinate(writingItemAt: file, options: .forDeleting, error: &error) { url in
@@ -115,7 +115,7 @@ internal struct DisplayReceiptCacheHelper {
115115
return nil
116116
}
117117

118-
static func cachedFiles() throws -> [URL] {
118+
func cachedFiles() throws -> [URL] {
119119
do {
120120
let cacheDir = try sharedDirectory()
121121
let urls = try FileManager.default.contentsOfDirectory(at: cacheDir,
@@ -148,7 +148,7 @@ internal struct DisplayReceiptCacheHelper {
148148

149149
// MARK: Methods reading user defaults
150150

151-
static func isOptOut() throws -> Bool {
151+
func isOptOut() throws -> Bool {
152152
let defaults = try self.sharedDefaults()
153153
if defaults.object(forKey: "batch_shared_optout") != nil {
154154
// Key is missing, we don't send display receipt

Sources/Swift/Display Receipt/DisplayReceiptHelper.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class DisplayReceiptHelper : NSObject {
4242
public func didReceive(_ request: UNNotificationRequest) {
4343

4444
do {
45-
if try DisplayReceiptCacheHelper.isOptOut() {
45+
if try DisplayReceiptCacheHelper().isOptOut() {
4646
print("Batch - SDK is opt-out, skipping display receipts")
4747
return
4848
}
@@ -162,19 +162,20 @@ extension DisplayReceiptHelper {
162162

163163
func save(_ receipt: DisplayReceipt) throws {
164164
let data = try receipt.pack()
165-
try DisplayReceiptCacheHelper.write(data)
165+
try DisplayReceiptCacheHelper().write(data)
166166
}
167167

168168
func send() throws {
169+
let cacheHelper = DisplayReceiptCacheHelper()
169170
// Read receipt in cache
170171
var packer = MessagePackWriter()
171-
let cachedFiles = try DisplayReceiptCacheHelper.cachedFiles()
172+
let cachedFiles = try cacheHelper.cachedFiles()
172173
var receipts = [MessagePackFlatValue]()
173174

174175
for file in cachedFiles {
175176
do {
176177
// Read and update cached receipt
177-
let data = try DisplayReceiptCacheHelper.read(fromFile: file)
178+
let data = try cacheHelper.read(fromFile: file)
178179
let receipt = try DisplayReceipt.unpack(from: data)
179180
receipt.sendAttempt += receipt.sendAttempt + 1
180181
receipt.replay = false
@@ -183,7 +184,7 @@ extension DisplayReceiptHelper {
183184
let tmpData = try MessagePackFlatValue {
184185
try receipt.pack(toWriter: &$0)
185186
}
186-
try DisplayReceiptCacheHelper.write(toFile: file, tmpData.data)
187+
try cacheHelper.write(toFile: file, tmpData.data)
187188

188189
receipts.append(tmpData)
189190
} catch {
@@ -215,7 +216,7 @@ extension DisplayReceiptHelper {
215216
if httpResponse != nil && httpResponse!.statusCode >= 200 && httpResponse!.statusCode <= 399 {
216217
// Request is successful - delete cached files
217218
for file in cachedFiles {
218-
let _ = DisplayReceiptCacheHelper.delete(file)
219+
let _ = cacheHelper.delete(file)
219220
}
220221
}
221222
})

0 commit comments

Comments
 (0)