OEmbed   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 22
eloc 52
c 3
b 0
f 0
dl 0
loc 156
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 8 1
A getOptions() 0 3 1
A __construct() 0 4 2
A getAmp() 0 3 1
A withOptions() 0 4 1
B getExtractor() 0 21 7
A get() 0 24 5
A findProviderMatch() 0 8 3
A setConfig() 0 9 1
1
<?php
2
namespace Cohensive\OEmbed;
3
4
use Cohensive\OEmbed\Exceptions\ExtractorException;
5
use Exception;
6
7
class OEmbed
8
{
9
    /**
10
     * AMP mode.
11
     */
12
    protected bool $amp = false;
13
14
    /**
15
     * Ignore possible exceptions occuring during OEmbed provider http requests.
16
     */
17
    protected bool $ignoreHttpErrors = true;
18
19
    /**
20
     * Options to apply to extracted embed objects.
21
     */
22
    protected array $options = [];
23
24
    /**
25
     * Quick access to list of OEmbed providers.
26
     */
27
    protected array $oembedProviders = [];
28
29
    /**
30
     * Quick access to list of non-OEmbed providers.
31
     */
32
    protected array $regexProviders = [];
33
34
    /**
35
     * Creates OEmbed instance.
36
     */
37
    public function __construct(?array $config = null)
38
    {
39
        if (is_array($config)) {
40
            $this->setConfig($config);
41
        }
42
    }
43
44
    /**
45
     * Parse URL and attempt to fetch embed data
46
     */
47
    public function get(string $url, array $parameters = []): ?Embed
48
    {
49
        $extractor = $this->getExtractor($url);
50
51
        if (!$extractor) {
52
            return null;
53
        }
54
55
        try {
56
            $embed = $extractor->fetch($parameters);
57
        } catch (Exception $e) {
58
            if ($this->ignoreHttpErrors) {
59
                return null;
60
            }
61
            throw new ExtractorException($e->getMessage());
62
        }
63
64
        if (!$embed) {
65
            return null;
66
        }
67
68
        $embed->setOptions($this->options)->setAmp($this->amp)->initData();
69
70
        return $embed;
71
    }
72
73
    /**
74
     * Set configs.
75
     */
76
    public function setConfig(array $config): self
77
    {
78
        $this->amp = $config['amp'] ?? false;
79
        $this->ignoreHttpErrors = $config['ignore_http_errors'] ?? true;
80
        $this->options = $config['options'] ?? [];
81
        $this->oembedProviders = $config['oembed_providers'] ?? [];
82
        $this->regexProviders = $config['regex_providers'] ?? [];
83
84
        return $this;
85
    }
86
87
    /**
88
     * Overrides instance options.
89
     */
90
    public function withOptions(array $options): self
91
    {
92
        $this->options = $options;
93
        return $this;
94
    }
95
96
    /**
97
     * Returns AMP mode.
98
     */
99
    public function getAmp(): bool
100
    {
101
        return $this->amp;
102
    }
103
104
    /**
105
     * Returns options array.
106
     */
107
    public function getOptions(): array
108
    {
109
        return $this->options;
110
    }
111
112
    /**
113
     * Restores embed from an array.
114
     */
115
    public function hydrate(array $data): Embed
116
    {
117
        return new Embed(
118
            $data['type'],
119
            $data['url'],
120
            $data['data'],
121
            $this->options,
122
            $this->amp
123
        );
124
    }
125
126
    /**
127
     * Returns a matched extractor for url.
128
     */
129
    protected function getExtractor(string $url): ?Extractor
130
    {
131
        foreach ($this->oembedProviders as $endpoint => $provider) {
132
            if (isset($provider['schemes'])) {
133
                if ($this->findProviderMatch($url, $provider['schemes'])) {
134
                    return new OEmbedExtractor($endpoint, $url, $provider['parameters'] ?? []);
135
                }
136
            } else {
137
                if ($this->findProviderMatch($url, $provider)) {
138
                    return new OEmbedExtractor($endpoint, $url);
139
                }
140
            }
141
        }
142
143
        foreach ($this->regexProviders as $provider) {
144
            if ($this->findProviderMatch($url, $provider['urls'])) {
145
                return new RegexExtractor($provider, $url);
146
            }
147
        }
148
149
        return null;
150
    }
151
152
    /**
153
     * Find a url match in provider pattern
154
     */
155
    protected function findProviderMatch($url, $patterns): bool
156
    {
157
        foreach ($patterns as $pattern) {
158
            if (!!preg_match($pattern, $url)) {
159
                return true;
160
            }
161
        }
162
        return false;
163
    }
164
}
165