Completed
Push — master ( 6323ac...52893e )
by
unknown
6s
created

helpers.php ➔ head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
use Spires\Core\Core;
5
use Spires\Irc\Client;
6
use Spires\Plugins\Message\Inbound\Message as InboundMessage;
7
use Spires\Plugins\Message\Outbound\Message;
8
use Spires\Plugins\SystemMessage\Outbound\SystemMessage;
9
10
function core(string $abstract = null, array $parameters = [])
11
{
12
    if (is_null($abstract)) {
13
        return Core::getInstance();
14
    }
15
16
    return Core::getInstance()->make($abstract, $parameters);
17
}
18
19
function send_command(string $command, string $params)
20
{
21
    core(Client::class)->write((string) new SystemMessage($command, $params));
22
}
23
24
function send_to(array $targets, string $text)
25
{
26
    core(Client::class)->write((string) new Message($targets, $text));
27
}
28
29
function reply(string $text)
30
{
31
    $myNick = core(Client::class)->user()->nickname();
32
    $senderNick = core(InboundMessage::class)->nickname();
33
    $targets = core(InboundMessage::class)->targets();
34
    $targets = array_map(function ($target) use ($myNick, $senderNick) {
35
        return $target === $myNick ? $senderNick : $target;
36
    }, $targets);
37
38
    send_to($targets, $text);
39
}
40
41
/**
42
 * Returns the first element of the given array
43
 *
44
 * @param array $array
45
 */
46
function head($array)
47
{
48
    return array_values($array)[0];
49
}
50