InfoCommand::run()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 32
rs 9.584
cc 3
nc 4
nop 2
1
<?php
2
namespace PortlandLabs\Slackbot\Command;
3
4
use PortlandLabs\Slackbot\Command\Argument\Manager;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PortlandLabs\Slackbot\Command\Manager. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use PortlandLabs\Slackbot\Slack\Api\Payload\ReactionsAddPayload;
6
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message;
7
use Symfony\Component\Console\Helper\Helper;
8
9
class InfoCommand extends SimpleCommand
10
{
11
12
    protected $description = 'Get some diagnostic information';
13
14
    protected $signature = 'info';
15
16
    /**
17
     * Handle a message
18
     *
19
     * @param Message $message
20
     * @param Manager $manager
21
     *
22
     * @return void
23
     * @throws \CL\Slack\Exception\SlackException
24
     * @throws \GuzzleHttp\Exception\GuzzleException
25
     */
26
    public function run(Message $message, Manager $manager)
27
    {
28
        $extensions = [];
29
        foreach (get_loaded_extensions() as $extension) {
30
            $extensions[] = '`' . $extension . '@' . phpversion($extension) . '`';
31
        }
32
        
33
        $details = [
34
            'ID' => '`' . $this->bot->getId() . '`',
35
            'Uptime' => '_' . $this->bot->getUptime() . '_',
36
            'Memory Usage' => Helper::formatMemory(memory_get_usage(true)),
37
            'Peak Usage' => Helper::formatMemory(memory_get_peak_usage(true)),
38
            'userId' => $this->bot->rtm()->getUserId(),
39
            'userName' => $this->bot->rtm()->getUserName(),
40
            'PHP Version' => phpversion(),
41
            'extensions' => implode(', ', $extensions),
42
        ];
43
44
        $data = [];
45
        foreach ($details as $key => $detail) {
46
            $data[] = "*$key:* $detail";
47
        }
48
49
        $api = $this->bot->api();
50
        $builder = $api->getBuilder();
51
52
        // Send with icon
53
        $builder->send(implode(PHP_EOL, $data))
54
            ->to($message->getChannel())->withIcon(':information_source:')
55
            ->execute($api);
56
57
        $this->bot->api()->send(ReactionsAddPayload::reactTo($message, 'the_horns'));
58
    }
59
}
60