From f054c8f2a01596a430bff338c550f5db9c815c58 Mon Sep 17 00:00:00 2001 From: jguz-pubnub Date: Mon, 25 Mar 2024 12:26:45 +0100 Subject: [PATCH] Quality gates (Codacy) --- .../DependencyContainer.swift | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/Sources/PubNub/DependencyContainer/DependencyContainer.swift b/Sources/PubNub/DependencyContainer/DependencyContainer.swift index bff0c2d8..82ca1b22 100644 --- a/Sources/PubNub/DependencyContainer/DependencyContainer.swift +++ b/Sources/PubNub/DependencyContainer/DependencyContainer.swift @@ -39,7 +39,7 @@ class DependencyContainer { // Each time the dependency is requested, a new instance is created and returned case transient } - + init(instanceID: UUID = UUID(), configuration: PubNubConfiguration) { register(value: configuration, forKey: PubNubConfigurationDependencyKey.self) register(value: instanceID, forKey: PubNubInstanceIDDependencyKey.self) @@ -55,32 +55,30 @@ class DependencyContainer { } subscript(key: K.Type) -> K.Value where K: DependencyKey { - get { - guard let underlyingKey = registeredKeys[ObjectIdentifier(key)] else { - preconditionFailure("Cannot find \(key). Ensure this key was registered before") - } - if underlyingKey.scope == .transient { - if let value = underlyingKey.key.value(from: self) as? K.Value { - return value - } else { - preconditionFailure("Cannot create value for key \(key)") - } - } - if let valueWrapper = resolvedValues[ObjectIdentifier(key)] { - if let underlyingValue = valueWrapper.value as? K.Value { - return underlyingValue - } - } + guard let underlyingKey = registeredKeys[ObjectIdentifier(key)] else { + preconditionFailure("Cannot find \(key). Ensure this key was registered before") + } + if underlyingKey.scope == .transient { if let value = underlyingKey.key.value(from: self) as? K.Value { - if Mirror(reflecting: value).displayStyle == .class && underlyingKey.scope == .weak { - resolvedValues[ObjectIdentifier(key)] = WeakWrapper(value as AnyObject) - } else { - resolvedValues[ObjectIdentifier(key)] = ValueWrapper(value) - } return value + } else { + preconditionFailure("Cannot create value for key \(key)") + } + } + if let valueWrapper = resolvedValues[ObjectIdentifier(key)] { + if let underlyingValue = valueWrapper.value as? K.Value { + return underlyingValue + } + } + if let value = underlyingKey.key.value(from: self) as? K.Value { + if Mirror(reflecting: value).displayStyle == .class && underlyingKey.scope == .weak { + resolvedValues[ObjectIdentifier(key)] = WeakWrapper(value as AnyObject) + } else { + resolvedValues[ObjectIdentifier(key)] = ValueWrapper(value) } - preconditionFailure("Cannot create value for key \(key)") + return value } + preconditionFailure("Cannot create value for key \(key)") } func register(key: K.Type, scope: Scope = .container) {