RssPlugin   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A rss() 0 10 1
1
<?php
2
3
namespace Nopolabs\Yabot\Plugins\Rss;
4
5
use Nopolabs\Yabot\Helpers\GuzzleTrait;
6
use Nopolabs\Yabot\Message\Message;
7
use Nopolabs\Yabot\Plugin\PluginInterface;
8
use Nopolabs\Yabot\Plugin\PluginTrait;
9
use Psr\Log\LoggerInterface;
10
11
class RssPlugin implements PluginInterface
12
{
13
    use PluginTrait;
14
    use GuzzleTrait;
15
16
    private $rssService;
17
18
    public function __construct(
19
        LoggerInterface $logger,
20
        RssService $rssService,
21
        array $config = [])
22
    {
23
        $this->setLog($logger);
24
        $this->rssService = $rssService;
25
        $this->setConfig(array_merge(
26
            [
27
                'help' => '<prefix> [raw|xml|json|object]? url',
28
                'prefix' => 'rss',
29
                'matchers' => [
30
                    'rss' => "/^(?:(?'format'raw|xml|json|object)\\s+)?(?'url'http[^\\s]+\\.xml)/",
31
                ],
32
            ],
33
            $config
34
        ));
35
    }
36
37
    public function rss(Message $msg, array $matches)
38
    {
39
        $url = $matches['url'];
40
41
        $this->rssService->fetchJson($url)->then(
42
            function (string $json) use ($msg) {
43
                $msg->reply($json);
44
            }
45
        );
46
    }
47
}