Skip to content

Commit f4ed32a

Browse files
authored
Merge pull request #116 from VivekKumar97/fix/cognitive-complexity
Refactored method to reduce cognitive complexity
2 parents 1b27c1d + 2146894 commit f4ed32a

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/main/java/com/mastercard/developer/encryption/EncryptionConfigBuilder.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,34 +54,40 @@ static byte[] sha256digestBytes(byte[] bytes) throws NoSuchAlgorithmException {
5454
}
5555

5656
void checkJsonPathParameterValues() {
57-
for (Map.Entry<String, String> entry : decryptionPaths.entrySet()) {
58-
if(entry.getKey().contains("[*]") || entry.getValue().contains("[*]")){
59-
if(!(entry.getKey().contains("[*]") && entry.getValue().contains("[*]"))){
60-
throw new IllegalArgumentException("JSON paths for decryption with wildcard must both contain a wildcard!");
61-
}
62-
if((entry.getKey().split("[*]", -1).length-1 > 1 || entry.getValue().split("[*]", -1).length-1 > 1)){
63-
throw new IllegalArgumentException("JSON paths for decryption with can only contain one wildcard!");
64-
}
65-
} else {
66-
if (!JsonPath.isPathDefinite(entry.getKey()) || !JsonPath.isPathDefinite(entry.getValue())) {
67-
throw new IllegalArgumentException("JSON paths for decryption must point to a single item!");
68-
}
69-
}
57+
decryptionPaths.forEach((key, value) -> validatePaths(key, value, "decryption"));
58+
encryptionPaths.forEach((key, value) -> validatePaths(key, value, "encryption"));
59+
}
60+
61+
private void validatePaths(String key, String value, String action) {
62+
boolean keyHasWildcard = key.contains("[*]");
63+
boolean valueHasWildcard = value.contains("[*]");
64+
if (keyHasWildcard || valueHasWildcard) {
65+
validateBothOrNoneHasWildcard(keyHasWildcard, valueHasWildcard, action);
66+
validateSingleWildcardOnly(key, value, action);
67+
} else {
68+
validateDefinitePaths(key, value, action);
7069
}
70+
}
7171

72-
for (Map.Entry<String, String> entry : encryptionPaths.entrySet()) {
73-
if(entry.getKey().contains("[*]") || entry.getValue().contains("[*]")){
74-
if(!(entry.getKey().contains("[*]") && entry.getValue().contains("[*]"))){
75-
throw new IllegalArgumentException("JSON paths for encryption with wildcard must both contain a wildcard!");
76-
}
77-
if((entry.getKey().split("[*]", -1).length-1 > 1 || entry.getValue().split("[*]", -1).length-1 > 1)){
78-
throw new IllegalArgumentException("JSON paths for encryption with can only contain one wildcard!");
79-
}
80-
} else {
81-
if (!JsonPath.isPathDefinite(entry.getKey()) || !JsonPath.isPathDefinite(entry.getValue())) {
82-
throw new IllegalArgumentException("JSON paths for encryption must point to a single item!");
83-
}
84-
}
72+
private void validateBothOrNoneHasWildcard(boolean keyHasWildcard, boolean valueHasWildcard, String action) {
73+
if (!(keyHasWildcard && valueHasWildcard)) {
74+
throw new IllegalArgumentException("JSON paths for " + action + " with wildcard must both contain a wildcard!");
8575
}
8676
}
77+
78+
private void validateSingleWildcardOnly(String key, String value, String action) {
79+
if (countWildcards(key) > 1 || countWildcards(value) > 1) {
80+
throw new IllegalArgumentException("JSON paths for " + action + " with can only contain one wildcard!");
81+
}
82+
}
83+
84+
private void validateDefinitePaths(String key, String value, String action) {
85+
if (!JsonPath.isPathDefinite(key) || !JsonPath.isPathDefinite(value)) {
86+
throw new IllegalArgumentException("JSON paths for " + action + " must point to a single item!");
87+
}
88+
}
89+
90+
private int countWildcards(String path) {
91+
return path.split("\\[\\*]", -1).length - 1;
92+
}
8793
}

0 commit comments

Comments
 (0)