CommandGroup   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 53
c 2
b 0
f 0
dl 0
loc 190
rs 10
wmc 24

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A __get() 0 6 2
A __isset() 0 9 3
A unserialize() 0 12 3
A reload() 0 3 2
A setEnabledIn() 0 17 5
A isEnabledIn() 0 7 3
A unload() 0 3 2
A serialize() 0 7 1
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\Commands;
11
12
/**
13
 * A group for commands.
14
 *
15
 * @property \CharlotteDunois\Livia\Client        $client         The client which initiated the instance.
16
 * @property string                               $id             The ID of the group.
17
 * @property string                               $name           The name of the group.
18
 * @property bool                                 $guarded        Whether this group is guarded against disabling.
19
 * @property \CharlotteDunois\Collect\Collection  $commands       The commands that the group contains.
20
 */
21
class CommandGroup implements \Serializable {
22
    /**
23
     * The client which initiated the instance.
24
     * @var \CharlotteDunois\Livia\Client
25
     */
26
    protected $client;
27
    
28
    /**
29
     * The ID of the group.
30
     * @var string
31
     */
32
    protected $id;
33
    
34
    /**
35
     * The name of the group.
36
     * @var string
37
     */
38
    protected $name;
39
    
40
    /**
41
     * Whether this group is guarded against disabling.
42
     * @var bool
43
     */
44
    protected $guarded;
45
    
46
    /**
47
     * The commands that the group contains.
48
     * @var \CharlotteDunois\Collect\Collection
49
     */
50
    protected $commands;
51
    
52
    /**
53
     * Whether the command group is globally enabled.
54
     * @var bool
55
     */
56
    protected $globalEnabled = true;
57
    
58
    /**
59
     * Array of guild ID to bool, which indicates whether the command group is enabled in that guild.
60
     * @var bool[]
61
     */
62
    protected $guildEnabled = array();
63
    
64
    /**
65
     * Constructs a new Command Group.
66
     * @param \CharlotteDunois\Livia\Client  $client
67
     * @param string                         $id
68
     * @param string                         $name
69
     * @param bool                           $guarded
70
     * @param array|null                     $commands
71
     */
72
    function __construct(\CharlotteDunois\Livia\Client $client, string $id, string $name, bool $guarded = false, array $commands = null) {
73
        $this->client = $client;
74
        
75
        $this->id = $id;
76
        $this->name = $name;
77
        $this->guarded = $guarded;
78
        
79
        $this->commands = new \CharlotteDunois\Collect\Collection();
80
        if(!empty($commands)) {
81
            foreach($commands as $command) {
82
                $this->commands->set($command->name, $command);
83
            }
84
        }
85
    }
86
    
87
    /**
88
     * @param string  $name
89
     * @return bool
90
     * @throws \Exception
91
     * @internal
92
     */
93
    function __isset($name) {
94
        try {
95
            return $this->$name !== null;
96
        } catch (\RuntimeException $e) {
97
            if($e->getTrace()[0]['function'] === '__get') {
98
                return false;
99
            }
100
            
101
            throw $e;
102
        }
103
    }
104
    
105
    /**
106
     * @param string  $name
107
     * @return mixed
108
     * @throws \RuntimeException
109
     * @internal
110
     */
111
    function __get($name) {
112
        if(\property_exists($this, $name)) {
113
            return $this->$name;
114
        }
115
        
116
        throw new \RuntimeException('Unknown property '.\get_class($this).'::$'.$name);
117
    }
118
    
119
    /**
120
     * @return string
121
     * @internal
122
     */
123
    function serialize() {
124
        $vars = \get_object_vars($this);
125
        
126
        unset($vars['client']);
127
        $vars['commands'] = new \CharlotteDunois\Collect\Collection();
128
        
129
        return \serialize($vars);
130
    }
131
    
132
    /**
133
     * @return void
134
     * @internal
135
     */
136
    function unserialize($vars) {
137
        if(\CharlotteDunois\Yasmin\Models\ClientBase::$serializeClient === null) {
138
            throw new \Exception('Unable to unserialize a class without ClientBase::$serializeClient being set');
139
        }
140
        
141
        $vars = \unserialize($vars);
142
        
143
        foreach($vars as $name => $val) {
144
            $this->$name = $val;
145
        }
146
        
147
        $this->client = \CharlotteDunois\Yasmin\Models\ClientBase::$serializeClient;
148
    }
149
    
150
    /**
151
     * Enables or disables the group in a guild.
152
     * @param string|\CharlotteDunois\Yasmin\Models\Guild|null  $guild  The guild instance or the guild ID.
153
     * @param bool                                              $enabled
154
     * @return bool
155
     * @throws \BadMethodCallException
156
     * @throws \InvalidArgumentException
157
     */
158
    function setEnabledIn($guild, bool $enabled) {
159
        if($guild !== null) {
160
            $guild = $this->client->guilds->resolve($guild);
161
        }
162
        
163
        if($this->guarded) {
164
            throw new \BadMethodCallException('The group is guarded');
165
        }
166
        
167
        if($guild !== null) {
168
            $this->guildEnabled[$guild->id] = $enabled;
169
        } else {
170
            $this->globalEnabled = $enabled;
171
        }
172
        
173
        $this->client->emit('groupStatusChange', $guild, $this, $enabled);
174
        return ($guild !== null ? $this->guildEnabled[$guild->id] : $this->globalEnabled);
175
    }
176
    
177
    /**
178
     * Checks if the group is enabled in a guild.
179
     * @param string|\CharlotteDunois\Yasmin\Models\Guild|null  $guild  The guild instance or the guild ID.
180
     * @return bool
181
     * @throws \InvalidArgumentException
182
     */
183
    function isEnabledIn($guild) {
184
        if($guild !== null) {
185
            $guild = $this->client->guilds->resolve($guild);
186
            return (!\array_key_exists($guild->id, $this->guildEnabled) || $this->guildEnabled[$guild->id]);
187
        }
188
        
189
        return $this->globalEnabled;
190
    }
191
    
192
    /**
193
     * Reloads all of the group's commands.
194
     * @return void
195
     * @throws \RuntimeException
196
     */
197
    function reload() {
198
        foreach($this->commands as $command) {
199
            $command->reload();
200
        }
201
    }
202
    
203
    /**
204
     * Unloads all of the group's commands.
205
     * @return void
206
     * @throws \RuntimeException
207
     */
208
    function unload() {
209
        foreach($this->commands as $command) {
210
            $command->unload();
211
        }
212
    }
213
}
214