YesOrNoPlugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 63
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A yes() 0 4 1
A no() 0 4 1
A maybe() 0 4 1
A questionMark() 0 4 1
A respond() 0 13 1
A url() 0 6 2
1
<?php
2
3
namespace Nopolabs\Yabot\Plugins\Examples;
4
5
6
use Nopolabs\Yabot\Guzzle\Guzzle;
7
use Nopolabs\Yabot\Helpers\GuzzleTrait;
8
use Nopolabs\Yabot\Message\Message;
9
use Nopolabs\Yabot\Plugin\PluginInterface;
10
use Nopolabs\Yabot\Plugin\PluginTrait;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Log\LoggerInterface;
13
14
class YesOrNoPlugin implements PluginInterface
15
{
16
    use PluginTrait;
17
    use GuzzleTrait;
18
19
    public function __construct(
20
        Guzzle $guzzle,
21
        LoggerInterface $logger)
22
    {
23
        $this->setGuzzle($guzzle);
24
        $this->setLog($logger);
25
26
        $this->setConfig([
27
            'matchers' => [
28
                'yes' => "/\\byes\\b/i",
29
                'no' => "/\\bno\\b/i",
30
                'maybe' => "/\\bmaybe\\b/i",
31
                'questionMark' => "/\?/i",
32
            ],
33
        ]);
34
    }
35
36
    public function yes(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...
37
    {
38
        $this->respond($msg, 'yes');
39
    }
40
41
    public function no(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...
42
    {
43
        $this->respond($msg, 'no');
44
    }
45
46
    public function maybe(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...
47
    {
48
        $this->respond($msg, 'maybe');
49
    }
50
51
    public function questionMark(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...
52
    {
53
        $this->respond($msg);
54
    }
55
56
    private function respond(Message $msg, $force = null)
57
    {
58
        $url = $this->url($force);
59
        $this->guzzle->getAsync($url)
60
            ->then(
61
                function (ResponseInterface $response) use ($msg) {
62
                    $json = $response->getBody();
63
                    $rsp = json_decode($json);
64
65
                    $msg->reply($rsp->image);
66
                }
67
            );
68
    }
69
70
    private function url($force = null)
71
    {
72
        $query = $force ? "?force=$force" : '';
73
74
        return "https://yesno.wtf/api/$query";
75
    }
76
}