help::information()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
/**
27
 * @property  message
28
 */
29
class help
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    public $config;
32
    public $discord;
33
    public $logger;
34
    private $excludeChannel;
35
    private $message;
36
    private $triggers;
37
38
    /**
39
     * @param $config
40
     * @param $discord
41
     * @param $logger
42
     */
43 View Code Duplication
    public function init($config, $discord, $logger)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->config = $config;
46
        $this->discord = $discord;
47
        $this->logger = $logger;
48
        $this->excludeChannel = $this->config['bot']['restrictedChannels'];
49
        $this->triggers[] = $this->config['bot']['trigger'] . 'help';
50
        $this->triggers[] = $this->config['bot']['trigger'] . 'Help';
51
    }
52
53
    /**
54
     * @param $msgData
55
     * @param $message
56
     * @return null
57
     */
58
    public function onMessage($msgData, $message)
59
    {
60
        $channelID = (int) $msgData['message']['channelID'];
61
62
        if (in_array($channelID, $this->excludeChannel, true))
63
        {
64
            return null;
65
        }
66
67
        $this->message = $message;
68
69
        $message = $msgData['message']['message'];
70
        $user = $msgData['message']['from'];
71
72
        $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']);
73
        if (isset($data['trigger'])) {
74
            global $plugins; // Need to have the plugins that are loaded available, yes it's ugly, whatever, better than shitting up the rest of the code :P
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
75
            $messageString = $data['messageString'];
76
77
            if (!$messageString) {
78
                // Show all modules available
79
                $commands = array();
80
                foreach ($plugins as $plugin) {
81
                    $info = $plugin->information();
82
                    if (!empty($info['name'])) {
83
                        $commands[] = $info['name'];
84
                    }
85
                }
86
87
                $this->message->reply('Here is a list of plugins available: **' . implode('** |  **', $commands) . "** If you'd like help with a specific plugin simply use the command !help <PluginName>");
88
                $this->logger->addInfo("Help: Sending help info to {$user}");
89
            } else {
90
                foreach ($plugins as $plugin) {
91
                    if (strtolower($messageString) === strtolower($plugin->information()['name'])) {
92
                        $this->message->reply($plugin->information()['information']);
93
                    }
94
                }
95
            }
96
        }
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function information()
103
    {
104
        return array(
105
            'name' => 'help',
106
            'trigger' => $this->triggers,
107
            'information' => 'Shows help for a plugin, or all the plugins available. Example: **!help pc**'
108
        );
109
    }
110
}