Passed
Push — master ( 0c6030...dc56c2 )
by Dan
01:55
created

GiphyPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 3
1
<?php
2
3
namespace Nopolabs\Yabot\Plugins\Giphy;
4
5
use Exception;
6
use GuzzleHttp\Promise\PromiseInterface;
7
use function GuzzleHttp\Promise\settle;
8
use Nopolabs\Yabot\Message\Message;
9
use Nopolabs\Yabot\Plugin\PluginInterface;
10
use Nopolabs\Yabot\Plugin\PluginTrait;
11
use Nopolabs\Yabot\Helpers\GuzzleTrait;
12
use Psr\Log\LoggerInterface;
13
14
class GiphyPlugin implements PluginInterface
15
{
16
    use PluginTrait;
17
    use GuzzleTrait;
18
19
    private $giphyService;
20
21 View Code Duplication
    public function __construct(
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...
22
        LoggerInterface $logger,
23
        GiphyService $giphyService,
24
        array $config = [])
25
    {
26
        $this->setLog($logger);
27
        $this->giphyService = $giphyService;
28
        $this->setConfig(array_merge(
29
            [
30
                'help' => '<prefix> [search terms] [optional format] (giphy formats to list available)',
31
                'prefix' => 'giphy',
32
                'matchers' => [
33
                    'each' => '/^\*\s+(.*)/',
34
                    'search' => '/^(.*)/',
35
                ],
36
                'format' => 'fixed_width',
37
            ],
38
            $config
39
        ));
40
    }
41
42
    public function each(Message $msg, array $matches)
43
    {
44
        $requests = [];
45
46
        $format = $this->getDefaultFormat();
47
48
        $terms = preg_split('/[\s,]+/', $matches[1]);
49
50
        foreach ($terms as $term) {
51
            $requests[$term] = $this->giphyService->search($term, $format);
52
        }
53
54
        $results = settle($requests)->wait();
55
        foreach ($results as $term => $result) {
56
            if ($result['state'] === PromiseInterface::FULFILLED) {
57
                $gifUrl = $result['value'];
58
                $msg->reply($term.': '.$gifUrl);
59
            }
60
        }
61
62
        $msg->setHandled(true);
63
    }
64
65
    public function search(Message $msg, array $matches)
66
    {
67
        if (trim($matches[1]) === 'formats') {
68
            $formats = $this->getFormats();
69
            $msg->reply('Available formats: ' . implode(' ', $formats));
70
            $msg->setHandled(true);
71
            return;
72
        }
73
74
        $terms = preg_split('/\s+/', $matches[1]);
75
        $query = $this->getQuery($terms);
76
        $format = $this->getFormat($terms);
77
78
        $this->giphyService->search($query, $format)->then(
79
            function (string $gifUrl) use ($msg) {
80
                $msg->reply($gifUrl);
81
            },
82
            function (Exception $e) {
83
                $this->getLog()->warning($e->getMessage());
84
            }
85
        );
86
87
        $msg->setHandled(true);
88
    }
89
90
    protected function getQuery(array $terms) : string
91
    {
92
        $search = [];
93
        $formats = $this->getFormats();
94
        foreach ($terms as $term) {
95
            if (!in_array($term, $formats)) {
96
                $search[] = $term;
97
            }
98
        }
99
        $query = implode(' ', $search);
100
101
        return $query;
102
    }
103
104
    protected function getFormat(array $terms) : string
105
    {
106
        $format = $this->getDefaultFormat();
107
        $formats = $this->getFormats();
108
        foreach ($terms as $term) {
109
            if (in_array($term, $formats)) {
110
                $format = $term;
111
            }
112
        }
113
114
        return $format;
115
    }
116
117
    protected function getDefaultFormat()
118
    {
119
        return $this->get('format', 'fixed_width');
120
    }
121
122
    protected function getFormats() : array
123
    {
124
        return $this->giphyService->getFormats();
125
    }
126
}