From 848d928238c985fd2060ce561b0708e22aedee4f Mon Sep 17 00:00:00 2001 From: Prateek Sachan Date: Mon, 16 Mar 2020 23:31:53 +0530 Subject: [PATCH] fixed high cyclomatic complexity --- clientapi/routing/pushrules.go | 93 ++++++++++++---------------------- 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/clientapi/routing/pushrules.go b/clientapi/routing/pushrules.go index 49b85eef7..4304d7e6b 100644 --- a/clientapi/routing/pushrules.go +++ b/clientapi/routing/pushrules.go @@ -99,78 +99,47 @@ func GetPushRule( util.GetLogger(req.Context()).WithError(err).Error("Could not unmarshal pushrules data") return jsonerror.InternalServerError() } - + pushRule := PushRule{} switch kind { case "override": - for _, pushRule := range pushRuleSet.Global.Override { - if pushRule.RuleId == ruleID { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: pushRule, - } - } - } - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("Not found"), - } + pushRule = getPushRulebyId(pushRuleSet.Global.Override, ruleID) + case "underride": - for _, pushRule := range pushRuleSet.Global.Underride { - if pushRule.RuleId == ruleID { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: pushRule, - } - } - } - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("Not found"), - } + pushRule = getPushRulebyId(pushRuleSet.Global.Underride, ruleID) + case "sender": - for _, pushRule := range pushRuleSet.Global.Sender { - if pushRule.RuleId == ruleID { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: pushRule, - } - } - } - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("Not found"), - } + pushRule = getPushRulebyId(pushRuleSet.Global.Sender, ruleID) + case "room": - for _, pushRule := range pushRuleSet.Global.Room { - if pushRule.RuleId == ruleID { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: pushRule, - } - } - } - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("Not found"), - } + pushRule = getPushRulebyId(pushRuleSet.Global.Room, ruleID) + case "content": - for _, pushRule := range pushRuleSet.Global.Content { - if pushRule.RuleId == ruleID { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: pushRule, - } - } - } - return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound("Not found"), - } + pushRule = getPushRulebyId(pushRuleSet.Global.Content, ruleID) + default: return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.NotFound("Unrecognised request"), } - + } + if pushRule.RuleId == ruleID { + return util.JSONResponse{ + Code: http.StatusOK, + JSON: pushRule, + } + } + return util.JSONResponse{ + Code: http.StatusNotFound, + JSON: jsonerror.NotFound("Not found"), } } + +//Returns a single push rule which matches given ruleID +func getPushRulebyId(pushRules []PushRule, ruleID string) PushRule { + for _, pushRule := range pushRules { + if pushRule.RuleId == ruleID { + return pushRule + } + } + return PushRule{} +}