Passed
Push — master ( 4bee7d...d5a095 )
by Vincent
04:06 queued 12s
created

DefaultCommandExecutor(ArgumentsHydrator)   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.admin.executor;
21
22
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
23
import fr.quatrevieux.araknemu.game.admin.Command;
24
import fr.quatrevieux.araknemu.game.admin.CommandParser;
25
import fr.quatrevieux.araknemu.game.admin.exception.AdminException;
26
import fr.quatrevieux.araknemu.game.admin.exception.CommandException;
27
import fr.quatrevieux.araknemu.game.admin.exception.CommandPermissionsException;
28
import fr.quatrevieux.araknemu.game.admin.executor.argument.ArgumentsHydrator;
29
30
/**
31
 * Base executor using {@link ArgumentsHydrator}
32
 */
33
public final class DefaultCommandExecutor implements CommandExecutor {
34
    private final ArgumentsHydrator hydrator;
35
36 1
    public DefaultCommandExecutor(ArgumentsHydrator hydrator) {
37 1
        this.hydrator = hydrator;
38 1
    }
39
40
    @Override
41
    public <A> void execute(Command<A> command, AdminPerformer performer, CommandParser.Arguments arguments) throws AdminException {
42 1
        if (!performer.isGranted(command.permissions())) {
43 1
            throw new CommandPermissionsException(command.name(), command.permissions());
44
        }
45
46 1
        A args = command.createArguments();
47
48 1
        if (!hydrator.supports(command, args)) {
49 1
            throw new CommandException(command.name(), "Cannot parse arguments for command " + command.getClass().getSimpleName());
50
        }
51
52
        try {
53 1
            args = hydrator.hydrate(command, args, arguments);
54 1
        } catch (AdminException ae) {
55 1
            throw ae;
56 1
        } catch (Exception e) {
57 1
            throw new CommandException(command.name(), e.getMessage());
58 1
        }
59
60 1
        command.execute(performer, args);
61 1
    }
62
}
63