Passed
Push — master ( cb960f...030734 )
by Roannel Fernández
03:54
created

getAvailableCommands()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
1
package com.github.netkorp.telegram.framework.managers;
2
3
import com.github.netkorp.telegram.framework.annotations.FreeCommand;
4
import com.github.netkorp.telegram.framework.bots.PollingTelegramBot;
5
import com.github.netkorp.telegram.framework.commands.interfaces.CloseCommand;
6
import com.github.netkorp.telegram.framework.commands.interfaces.Command;
7
import com.github.netkorp.telegram.framework.commands.interfaces.DoneCommand;
8
import com.github.netkorp.telegram.framework.commands.interfaces.HelpCommand;
9
import com.github.netkorp.telegram.framework.commands.interfaces.MultistageCommand;
10
import com.github.netkorp.telegram.framework.exceptions.CommandNotActive;
11
import com.github.netkorp.telegram.framework.exceptions.CommandNotFound;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.stereotype.Component;
15
16
import java.util.Arrays;
17
import java.util.Collection;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
22
@Component
23
public class CommandManager {
24
    private final Map<String, Command> commands;
25
    private final Map<String, Command> freeCommands;
26
27
    private final Map<Long, MultistageCommand> activeCommand;
28
29
    private String closeCommand;
30
    private String doneCommand;
31
    private String helpCommand;
32
33
    @Autowired
34
    public CommandManager(List<Command> commands, PollingTelegramBot bot, @Value("${telegram.commands.free}") String freeCommands) {
35
        this.commands = new HashMap<>();
36
        this.freeCommands = new HashMap<>();
37
38
        this.activeCommand = new HashMap<>();
39
40
        commands.forEach(command -> addCommand(command, bot, Arrays.asList(freeCommands.split(","))));
41
    }
42
43
    private void addCommand(Command command, PollingTelegramBot bot, List<String> freeCommandNames) {
44
        command.setBot(bot);
45
        command.setCommandManager(this);
46
        this.commands.put(command.command(), command);
47
48
        if (isFreeCommand(command, freeCommandNames)) {
49
            this.freeCommands.put(command.command(), command);
50
        }
51
52
        if (command instanceof CloseCommand) {
53
            closeCommand = command.command();
54
        } else if (command instanceof DoneCommand) {
55
            doneCommand = command.command();
56
        } else if (command instanceof HelpCommand) {
57
            helpCommand = command.command();
58
        }
59
    }
60
61
    private boolean isFreeCommand(Command command, List<String> freeCommandNames) {
62
        return freeCommandNames.contains(command.getName())
63
                || freeCommandNames.contains(command.command())
64
                || command.getClass().isAnnotationPresent(FreeCommand.class);
65
    }
66
67
    /**
68
     * Returns the {@link Command} instance from a given getName.
69
     *
70
     * @param command The commands as {@link String}.
71
     * @return The {@link Command} instance.
72
     * @throws CommandNotFound If the given name is not related to any commands.
73
     */
74
    public Command getCommand(String command) throws CommandNotFound {
75
        if (!this.commands.containsKey(command)) {
76
            throw new CommandNotFound();
77
        }
78
79
        return this.commands.get(command);
80
    }
81
82
    public Command getFreeCommand(String command) throws CommandNotFound {
83
        if (!this.freeCommands.containsKey(command)) {
84
            throw new CommandNotFound();
85
        }
86
87
        return this.freeCommands.get(command);
88
    }
89
90
    public void setActiveCommand(final Long idChat, final MultistageCommand command) {
91
        this.activeCommand.put(idChat, command);
92
    }
93
94
    /**
95
     * Gets the active commands for a given user.
96
     *
97
     * @param idChat Chat id of the user.
98
     * @return The active commands.
99
     */
100
    public MultistageCommand getActiveCommand(Long idChat) throws CommandNotActive {
101
        if (!hasActiveCommand(idChat)) {
102
            throw new CommandNotActive();
103
        }
104
105
        return activeCommand.get(idChat);
106
    }
107
108
    /**
109
     * Removes the active commands.
110
     *
111
     * @param idChat Chat id of the user.
112
     */
113
    public void removeActiveCommand(Long idChat) {
114
        activeCommand.remove(idChat);
115
    }
116
117
    /**
118
     * Is there an active commands or not?
119
     *
120
     * @param idChat Chat id of the user.
121
     * @return If there is active commands or not.
122
     */
123
    public boolean hasActiveCommand(Long idChat) {
124
        return activeCommand.containsKey(idChat);
125
    }
126
127
    public Command getCloseCommand() throws CommandNotFound {
128
        return getCommand(this.closeCommand);
129
    }
130
131
    public Command getDoneCommand() throws CommandNotFound {
132
        return getCommand(this.doneCommand);
133
    }
134
135
    public Command getHelpCommand() throws CommandNotFound {
136
        return getCommand(this.helpCommand);
137
    }
138
139
    /**
140
     * Returns the list with the available commands.
141
     *
142
     * @return Available commands.
143
     */
144
    public Collection<Command> getAvailableCommands() {
145
        return commands.values();
146
    }
147
}
148