Passed
Pull Request — 4 (#10244)
by Steve
05:51
created

EmbedContainer   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
dl 0
loc 159
rs 9.84
c 1
b 0
f 0
wmc 32

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeight() 0 5 3
A __construct() 0 3 1
A getWidth() 0 5 3
A getType() 0 16 5
A getCrawler() 0 3 1
A getEmbed() 0 6 2
A validate() 0 3 1
A getName() 0 10 3
B getPreviewURL() 0 32 8
A setOptions() 0 4 1
A getOptions() 0 3 1
A getExtractor() 0 6 2
A setCrawler() 0 4 1
1
<?php
2
3
namespace SilverStripe\View\Embed;
4
5
use Embed\Extractor;
6
use Embed\Embed;
7
use Embed\EmbedCode;
8
use Embed\Http\Crawler;
9
use Psr\Http\Message\UriInterface;
10
use SilverStripe\Core\Manifest\ModuleResourceLoader;
11
12
class EmbedContainer implements Embeddable
13
{
14
    private ?Embed $embed = null;
15
16
    private ?Extractor $extractor = null;
17
18
    private ?Crawler $crawler = null;
19
20
    private string $url;
21
22
    private array $options = [];
23
24
    public function __construct(string $url)
25
    {
26
        $this->url = $url;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function getWidth()
33
    {
34
        /** @var EmbedCode $code */
35
        $code = $this->getExtractor()->code;
36
        return $code ? ($code->width ?: 100) : 100;
0 ignored issues
show
introduced by
$code is of type Embed\EmbedCode, thus it always evaluated to true.
Loading history...
37
    }
38
39
    /**
40
     * @return int
41
     */
42
    public function getHeight()
43
    {
44
        /** @var EmbedCode $code */
45
        $code = $this->getExtractor()->code;
46
        return $code ? ($code->height ?: 100) : 100;
0 ignored issues
show
introduced by
$code is of type Embed\EmbedCode, thus it always evaluated to true.
Loading history...
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getPreviewURL()
53
    {
54
        $extractor = $this->getExtractor();
55
56
        // Use thumbnail url
57
        if ($extractor->image) {
58
            /** @var UriInterface $image */
59
            $image = $extractor->image;
60
            $query = $image->getQuery();
61
            $fragment = $image->getFragment();
62
            return sprintf(
63
                '%s://%s%s%s%s',
64
                $image->getScheme(),
65
                $image->getHost(),
66
                '/' . ltrim($image->getPath(), '/'),
67
                $query ? "?$query" : '',
68
                $fragment ? "#$fragment" : '',
69
            );
70
        }
71
72
        // Use direct image type
73
        $method = 'getApi';
74
        if (method_exists($extractor, $method)) {
75
            $api = $extractor->$method();
76
            if ($api && method_exists($api, 'url') && $api->url('direct_link')) {
77
                return $api->url('direct_link'); // <<< TODO test this works
78
            }
79
        }
80
81
        // Default media
82
        return ModuleResourceLoader::resourceURL(
83
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
84
        );
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getName()
91
    {
92
        $extractor = $this->getExtractor();
93
        if ($extractor->title) {
94
            return $extractor->title;
95
        }
96
        if ($extractor->url instanceof UriInterface) {
0 ignored issues
show
introduced by
$extractor->url is always a sub-type of Psr\Http\Message\UriInterface.
Loading history...
97
            return preg_replace('/\?.*/', '', basename($extractor->url->getPath()));
98
        }
99
        return '';
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getType()
106
    {
107
        $html = $this->getExtractor()->code->html ?? '';
108
        if (strpos($html, '<video') !== false) {
109
            return 'video';
110
        }
111
        if (strpos($html, '<iframe') !== false) {
112
            return 'rich';
113
        }
114
        if (strpos($html, '<audio') !== false) {
115
            return 'audio';
116
        }
117
        if (strpos($html, '<img') !== false) {
118
            return 'photo';
119
        }
120
        return 'link';
121
        // case 'video':
122
        // case 'rich':
123
        // case 'link':
124
        // case 'photo':
125
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function validate()
131
    {
132
        return !empty($this->getExtractor()->code->html ?? '');
133
    }
134
135
    public function setOptions(array $options): Embeddable
136
    {
137
        $this->options = $options;
138
        return $this;
139
    }
140
141
    public function getOptions(): array
142
    {
143
        return $this->options;
144
    }
145
146
    public function getEmbed(): Embed
147
    {
148
        if (!$this->embed) {
149
            $this->embed = new Embed($this->getCrawler());
150
        }
151
        return $this->embed;
152
    }
153
154
    public function getExtractor(): Extractor
155
    {
156
        if (!$this->extractor) {
157
            $this->extractor = $this->getEmbed()->get($this->url);
158
        }
159
        return $this->extractor;
160
    }
161
162
    public function setCrawler(?Crawler $crawler): self
163
    {
164
        $this->crawler = $crawler;
165
        return $this;
166
    }
167
168
    public function getCrawler(): ?Crawler
169
    {
170
        return $this->crawler;
171
    }
172
}
173