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

GiphyService::extractGifUrl()   A

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\Giphy;
4
5
use GuzzleHttp;
6
use GuzzleHttp\Promise\PromiseInterface;
7
use Nopolabs\Yabot\Guzzle\Guzzle;
8
use Nopolabs\Yabot\Helpers\ConfigTrait;
9
use Nopolabs\Yabot\Helpers\GuzzleTrait;
10
use Nopolabs\Yabot\Helpers\LogTrait;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Log\LoggerInterface;
13
14
class GiphyService
15
{
16
    use LogTrait;
17
    use GuzzleTrait;
18
    use ConfigTrait;
19
20
    public function __construct(
21
        LoggerInterface $logger,
22
        Guzzle $guzzle,
23
        array $config = [])
24
    {
25
        $this->setLog($logger);
26
        $this->setGuzzle($guzzle);
27
        $this->setConfig(array_merge(
28
            [
29
                'api_endpoint' => 'http://api.giphy.com/v1/gifs/search',
30
                'parameters' => [
31
                    'api_key' => 'dc6zaTOxFJmzC',
32
                    'limit' => 1,
33
                    'rating' => 'pg-13',
34
                ],
35
                'formats' => [
36
                    'fixed_height',
37
                    'fixed_height_still',
38
                    'fixed_height_downsampled',
39
                    'fixed_width',
40
                    'fixed_width_still',
41
                    'fixed_width_downsampled',
42
                    'fixed_height_small',
43
                    'fixed_height_small_still',
44
                    'fixed_width_small',
45
                    'fixed_width_small_still',
46
                    'preview',
47
                    'downsized_small',
48
                    'downsized',
49
                    'downsized_medium',
50
                    'downsized_large',
51
                    'downsized_still',
52
                    'original',
53
                    'original_still',
54
                    'looping',
55
                ],
56
            ],
57
            $config
58
        ));
59
    }
60
61
    public function getFormats()
62
    {
63
        return $this->get('formats');
64
    }
65
66
    public function search(string $query, string $format = 'fixed_width') : PromiseInterface
67
    {
68
        $endpoint = $this->get('api_endpoint');
69
        $params = $this->get('parameters');
70
        $params['q'] = $query;
71
        $url = $endpoint.'?'.http_build_query($params);
72
73
        $this->getLog()->info($url);
74
75
        return $this->getAsync($url)->then(
76
            function (ResponseInterface $response) use ($format) {
77
                return $this->extractGifUrl($format, $response);
78
            }
79
        );
80
    }
81
82
    private function extractGifUrl($format, ResponseInterface $response)
83
    {
84
        $data = GuzzleHttp\json_decode($response->getBody(), true)['data'];
85
86
        return $data[0]['images'][$format]['url'];
87
    }
88
}