CommandArgumentType   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 36
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A parse() 0 7 2
A validate() 0 11 3
1
<?php
2
/**
3
 * Livia
4
 * Copyright 2017-2019 Charlotte Dunois, All Rights Reserved
5
 *
6
 * Website: https://charuru.moe
7
 * License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE
8
*/
9
10
namespace CharlotteDunois\Livia\Types;
11
12
/**
13
 * {@inheritdoc}
14
 * @internal
15
 */
16
class CommandArgumentType extends ArgumentType {
17
    /**
18
     * @internal
19
     */
20
    function __construct(\CharlotteDunois\Livia\Client $client) {
21
        parent::__construct($client, 'command');
22
    }
23
    
24
    /**
25
     * {@inheritdoc}
26
     * @return bool|string|\React\Promise\ExtendedPromiseInterface
27
     */
28
    function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
29
        $commands = $this->client->registry->findCommands($value);
30
        if(\count($commands) === 1) {
31
            return true;
32
        }
33
        
34
        if(\count($commands) === 0) {
35
            return false;
36
        }
37
        
38
        return \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($commands, 'commands', 'name').\PHP_EOL;
39
    }
40
    
41
    /**
42
     * {@inheritdoc}
43
     * @return mixed|null|\React\Promise\ExtendedPromiseInterface
44
     */
45
    function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
46
        $commands = $this->client->registry->findCommands($value);
47
        if(\count($commands) > 0) {
48
            return $commands[0];
49
        }
50
        
51
        return null;
52
    }
53
}
54