UserArgumentType   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 45
c 7
b 0
f 0
dl 0
loc 92
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B validate() 0 44 7
A parse() 0 30 5
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 UserArgumentType extends ArgumentType {
17
    /**
18
     * @internal
19
     */
20
    function __construct(\CharlotteDunois\Livia\Client $client) {
21
        parent::__construct($client, 'user');
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
        $prg = \preg_match('/(?:<@!?)?(\d{15,})>?/', $value, $matches);
30
        if($prg === 1) {
31
            return $context->client->fetchUser($matches[1])->then(function () {
32
                return true;
33
            }, function () {
34
                return false;
35
            });
36
        }
37
        
38
        $search = \mb_strtolower($value);
39
        
40
        $inexactUsers = $this->client->users->filter(function ($user) use ($search) {
41
            return (\mb_stripos($user->tag, $search) !== false);
42
        });
43
        $inexactLength = $inexactUsers->count();
44
        
45
        if($inexactLength === 0) {
46
             return false;
47
        }
48
        if($inexactLength === 1) {
49
            return true;
50
        }
51
        
52
        $exactUsers = $this->client->users->filter(function ($user) use ($search) {
53
            return (\mb_strtolower($user->tag) === $search);
54
        });
55
        $exactLength = $exactUsers->count();
56
        
57
        if($exactLength === 1) {
58
            return true;
59
        }
60
        
61
        if($exactLength > 0) {
62
            $users = $exactUsers;
63
        } else {
64
            $users = $inexactUsers;
65
        }
66
        
67
        if($users->count() >= 15) {
68
            return 'Multiple users found. Please be more specific.';
69
        }
70
        
71
        return \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($users, 'users', 'tag').\PHP_EOL;
72
    }
73
    
74
    /**
75
     * {@inheritdoc}
76
     * @return mixed|null|\React\Promise\ExtendedPromiseInterface
77
     */
78
    function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) {
79
        $prg = \preg_match('/(?:<@!?)?(\d{15,})>?/', $value, $matches);
80
        if($prg === 1) {
81
            return $this->client->users->get($matches[1]);
82
        }
83
        
84
        $search = \mb_strtolower($value);
85
        
86
        $inexactUsers = $this->client->users->filter(function ($user) use ($search) {
87
            return (\mb_stripos($user->tag, $search) !== false);
88
        });
89
        $inexactLength = $inexactUsers->count();
90
        
91
        if($inexactLength === 0) {
92
             return null;
93
        }
94
        if($inexactLength === 1) {
95
            return $inexactUsers->first();
96
        }
97
        
98
        $exactUsers = $this->client->users->filter(function ($user) use ($search) {
99
            return (\mb_strtolower($user->tag) === $search);
100
        });
101
        $exactLength = $exactUsers->count();
102
        
103
        if($exactLength === 1) {
104
            return $exactUsers->first();
105
        }
106
        
107
        return null;
108
    }
109
}
110