OEmbedExtractor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 2
b 0
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 19 4
A __construct() 0 5 1
1
<?php
2
namespace Cohensive\OEmbed;
3
4
use Cohensive\OEmbed\Exceptions\ExtractorException;
5
6
class OEmbedExtractor extends Extractor
7
{
8
    public function __construct(string $provider, string $url, array $parameters = [])
9
    {
10
        $this->provider = $provider;
11
        $this->url = $url;
12
        $this->parameters = $parameters;
13
    }
14
15
    /**
16
     * Fetches OEmbed data from provider.
17
     */
18
    public function fetch(array $parameters = []): ?Embed
19
    {
20
        $data = array_merge($parameters ?: $this->parameters, ['url' => $this->url]);
21
        $query = http_build_query($data);
22
        $response = file_get_contents((string) $this->provider . '?' . $query);
23
24
        if (!$response) {
25
            return null;
26
        }
27
28
        $data = json_decode($response, true);
29
30
        if (!$data) {
31
            throw new ExtractorException('Invalid JSON response from OEmbed provider. Url: ' . $this->url);
32
        }
33
34
        $embed = new Embed(Embed::TYPE_OEMBED, $this->url, $data);
35
36
        return $embed;
37
    }
38
}
39