QueuePlugin::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Nopolabs\Yabot\Plugins\Queue;
4
5
use Nopolabs\Yabot\Message\Message;
6
use Nopolabs\Yabot\Plugin\PluginInterface;
7
use Nopolabs\Yabot\Plugin\PluginTrait;
8
use Psr\Log\LoggerInterface;
9
10
class QueuePlugin implements PluginInterface
11
{
12
    use PluginTrait;
13
14
    /** @var Queue */
15
    protected $queue;
16
17
    public function __construct(
18
        LoggerInterface $logger,
19
        Queue $queue,
20
        array $config = [])
21
    {
22
        $this->setLog($logger);
23
        $this->queue = $queue;
24
25
        $help = <<<EOS
26
<prefix> push #[pr]
27
<prefix> insert #[pr] [index]
28
<prefix> next
29
<prefix> rm #[pr]
30
<prefix> clear
31
<prefix> list
32
EOS;
33
34
        $this->setConfig(array_merge(
35
            [
36
                'help' => $help,
37
                'matchers' => [
38
                    'push' => "/^push\\s+#?(?'item'[0-9]{4,5})\\b/",
39
                    'insert' => "/^insert\\s+#?(?'item'[0-9]{4,5})(?:\\s+(?'index'\d+))?\\b/",
40
                    'next' => '/^next$/',
41
                    'remove' => "/^rm #?(?'item'[0-9]{4,5})\\b/",
42
                    'clear' => '/^clear$/',
43
                    'list' => '/^list$/',
44
                ],
45
            ],
46
            $config
47
        ));
48
    }
49
50
    public function push(Message $msg, array $matches)
51
    {
52
        $element = $this->queue->buildElement($msg, $matches);
53
54
        $this->queue->push($element);
55
56
        $this->list($msg);
57
    }
58
59
    public function insert(Message $msg, array $matches)
60
    {
61
        $element = $this->queue->buildElement($msg, $matches);
62
63
        $index = (int) $matches['index'] ?? 0;
64
65
        $this->queue->insert($element, $index);
66
67
        $this->list($msg);
68
    }
69
70
    public function next(Message $msg, array $matches)
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        $this->queue->next();
73
74
        $this->list($msg);
75
    }
76
77
    public function remove(Message $msg, array $matches)
78
    {
79
        $element = $this->queue->buildElement($msg, $matches);
80
81
        $this->queue->remove($element);
82
83
        $this->list($msg);
84
    }
85
86
    public function clear(Message $msg, array $matches)
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        $this->queue->clear();
89
90
        $this->list($msg);
91
    }
92
93
    public function list(Message $msg, array $matches = [])
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        $details = $this->queue->getDetails();
96
        if (empty($details)) {
97
            $details = ['The queue is empty.'];
98
        }
99
100
        $msg->reply(implode("\n", $details));
101
        $msg->setHandled(true);
102
    }
103
}