Completed
Branch develop (e3a612)
by Patryk
10:42 queued 45s
created

IframelyExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDataFromApi() 0 7 1
A getEmbedUrl() 0 4 1
A getEmbedHtml() 0 4 1
A getFilters() 0 7 1
A getName() 0 4 1
1
<?php
2
3
namespace KaLLoSz\Twig\Extension;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class IframelyExtension
9
 * @package KaLLoSz\Twig\Extension
10
 *
11
 * @author Patryk Kala <[email protected]>
12
 */
13
class IframelyExtension extends \Twig_Extension
14
{
15
    /**
16
     * @var IframelyClientInterface
17
     */
18
    protected $client;
19
20
    /**
21
     * @param IframelyClientInterface $client Iframely Client
22
     */
23
    public function __construct(IframelyClientInterface $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    /**
29
     * @param string $url
30
     *
31
     * @return IframelyDTO
32
     */
33
    private function getDataFromApi(string $url): IframelyDTO
34
    {
35
        /** @var ResponseInterface $data */
36
        $data = $this->client->getUrlData($url);
37
38
        return new IframelyDTO(json_decode($data->getBody()->getContents(), true));
39
    }
40
41
    /**
42
     * @param string $url
43
     *
44
     * @return string
45
     */
46
    public function getEmbedUrl(string $url): string
47
    {
48
        return $this->getDataFromApi($url);
49
    }
50
51
    /**
52
     * @param string $url
53
     *
54
     * @return string
55
     */
56
    public function getEmbedHtml(string $url): string
57
    {
58
        return $this->getDataFromApi($url)->getHtml();
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getFilters()
65
    {
66
        return [
67
            new \Twig_SimpleFilter('iframelyUrl', [$this, 'getEmbedUrl']),
68
            new \Twig_SimpleFilter('iframelyHtml', [$this, 'getEmbedHtml']),
69
        ];
70
    }
71
72
    public function getName()
73
    {
74
        return 'kallosz\twig-iframely-extension';
75
    }
76
}
77