OEmbedExtractor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
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