Skip to content

Commit

Permalink
Quality gates (Codacy)
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Mar 25, 2024
1 parent 2ba4084 commit f054c8f
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions Sources/PubNub/DependencyContainer/DependencyContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -55,32 +55,30 @@ class DependencyContainer {
}

subscript<K>(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<K: DependencyKey>(key: K.Type, scope: Scope = .container) {
Expand Down

0 comments on commit f054c8f

Please sign in to comment.