OSDN Git Service

不要となっていたプロパティを削除
[kcd/KCD.git] / KCD / CommandRegister.swift
index 237abe1..5b87b5b 100644 (file)
@@ -9,13 +9,16 @@
 import Cocoa
 
 enum JSONCommandError: Error {
-    case CanNotFindCommand
+    
+    case canNotFindCommand
 }
 
-class CommandRegister {
+final class CommandRegister {
     
     private static var registeredClasses: [JSONCommand.Type] = []
+    
     class func register() {
+        
         registerClass(Start2Command.self)
         registerClass(MemberNDockCommand.self)
         registerClass(MemberKDockCommand.self)
@@ -56,38 +59,46 @@ class CommandRegister {
         registerClass(AirCorpsChangeNameCommand.self)
     }
     
-    class func command(for response: APIResponse) throws -> JSONCommand {
-        var command: JSONCommand? = nil
-        if !response.success {
-            command = FailedCommand(apiResponse: response)
-        }
-        for c in registeredClasses {
-            if c.canExecuteAPI(response.api) {
-                command = c.init(apiResponse: response)
-                break
-            }
-        }
+    class func command(for response: APIResponse) -> JSONCommand {
         
-        if command == nil {
+        func concreteCommand(for response: APIResponse) -> JSONCommand {
+            
+            if !response.success {
+                
+                return FailedCommand(apiResponse: response)
+            }
+            
             if IgnoreCommand.canExecuteAPI(response.api) {
-                command = IgnoreCommand(apiResponse: response)
+                
+                return IgnoreCommand(apiResponse: response)
             }
+            
+            if let c = registeredClasses.first(where: { $0.canExecuteAPI(response.api) }) {
+                
+                return c.init(apiResponse: response)
+            }
+            
+            return UnknownComand(apiResponse: response)
         }
-        if command == nil {
-            command = UnknownComand(apiResponse: response)
-        }
-        
-        guard var validCommand = command
-            else { throw JSONCommandError.CanNotFindCommand }
         
         #if ENABLE_JSON_LOG
-            validCommand = JSONViewCommand(apiResponse: response, command: validCommand)
-        #endif
         
-        return validCommand
+            return JSONViewCommand(apiResponse: response, command: concreteCommand(for: response))
+        
+        #else
+        
+            return concreteCommand(for: response)
+        
+        #endif
     }
+    
     class func registerClass(_ commandClass: JSONCommand.Type) {
-        if registeredClasses.contains(where: { $0 == commandClass }) { return }
+        
+        if registeredClasses.contains(where: { $0 == commandClass }) {
+            
+            return
+        }
+        
         registeredClasses.append(commandClass)
     }
 }