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

GiphyPlugin   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 17.7 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 20
loc 113
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 20 20 1
B each() 0 22 4
B search() 0 24 2
A getQuery() 0 13 3
A getFormat() 0 12 3
A getDefaultFormat() 0 4 1
A getFormats() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}