Passed
Push — master ( 2f2f80...76d58f )
by Roannel Fernández
02:54
created

com.github.netkorp.telegram.framework.condition.ExcludeCondition   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A matches(ConditionContext,AnnotatedTypeMetadata) 0 12 4
1
package com.github.netkorp.telegram.framework.condition;
2
3
import com.github.netkorp.telegram.framework.annotations.TelegramCommand;
4
import org.springframework.context.annotation.Condition;
5
import org.springframework.context.annotation.ConditionContext;
6
import org.springframework.core.type.AnnotatedTypeMetadata;
7
import org.springframework.core.type.AnnotationMetadata;
8
import org.springframework.core.type.MethodMetadata;
9
import org.springframework.stereotype.Component;
10
11
import java.util.Arrays;
12
import java.util.Map;
13
14
@Component
15
public class ExcludeCondition implements Condition {
16
17
    /**
18
     * Determine if the condition matches.
19
     *
20
     * @param context  the condition context
21
     * @param metadata metadata of the {@link AnnotationMetadata class}
22
     *                 or {@link MethodMetadata method} being checked
23
     * @return {@code true} if the condition matches and the component can be registered,
24
     * or {@code false} to veto the annotated component's registration
25
     */
26
    @Override
27
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
28
        String excludeValue = context.getEnvironment().getProperty("telegram.commands.exclude");
29
        if (excludeValue != null && context.getBeanFactory() != null) {
30
            Map<String, Object> attributes = metadata.getAnnotationAttributes(TelegramCommand.class.getName());
31
            if (attributes != null) {
32
                String commandName = (String) attributes.get("name");
33
                return !Arrays.asList(excludeValue.split(",")).contains(commandName);
34
            }
35
        }
36
37
        return true;
38
    }
39
}
40