Passed
Pull Request — 4 (#10244)
by Steve
15:32
created

EmbedContainer::getPreviewURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace SilverStripe\View\Embed;
4
5
use Embed\Extractor;
6
use Embed\Embed;
7
use Embed\Http\Crawler;
8
use Psr\Http\Message\UriInterface;
9
use SilverStripe\Core\Manifest\ModuleResourceLoader;
10
11
/**
12
 * This class acts as a wrapper around the third party requirement embed/embed v4
13
 */
14
class EmbedContainer implements Embeddable
15
{
16
    private ?Embed $embed = null;
17
18
    private ?Extractor $extractor = null;
19
20
    private ?Crawler $crawler = null;
21
22
    private string $url;
23
24
    private array $options = [];
25
26
    public function __construct(string $url)
27
    {
28
        $this->url = $url;
29
    }
30
31
    /**
32
     * @return int
33
     */
34
    public function getWidth()
35
    {
36
        $code = $this->getExtractor()->code;
37
        return $code ? ($code->width ?: 100) : 100;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    public function getHeight()
44
    {
45
        $code = $this->getExtractor()->code;
46
        return $code ? ($code->height ?: 100) : 100;
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
            return (string) $extractor->image;
59
        }
60
61
        // Default media
62
        return ModuleResourceLoader::resourceURL(
63
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
64
        );
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getName()
71
    {
72
        $extractor = $this->getExtractor();
73
        if ($extractor->title) {
74
            return $extractor->title;
75
        }
76
        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...
77
            return basename($extractor->url->getPath());
78
        }
79
        return '';
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getType()
86
    {
87
        $html = $this->getExtractor()->code->html ?? '';
88
        if (strpos($html, '<video') !== false) {
89
            return 'video';
90
        }
91
        if (strpos($html, '<audio') !== false) {
92
            return 'audio';
93
        }
94
        foreach (['iframe', 'blockquote', 'pre', 'script', 'style'] as $richTag) {
95
            if (strpos($html, "<{$richTag}") !== false) {
96
                return 'rich';
97
            }
98
        }
99
        if (strpos($html, '<img') !== false) {
100
            return 'photo';
101
        }
102
        return 'link';
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function validate()
109
    {
110
        return !empty($this->getExtractor()->code->html ?? '');
111
    }
112
113
    public function setOptions(array $options): self
114
    {
115
        $this->options = $options;
116
        return $this;
117
    }
118
119
    public function getOptions(): array
120
    {
121
        return $this->options;
122
    }
123
124
    public function getEmbed(): Embed
125
    {
126
        if (!$this->embed) {
127
            $this->embed = new Embed($this->getCrawler());
128
        }
129
        return $this->embed;
130
    }
131
132
    public function getExtractor(): Extractor
133
    {
134
        if (!$this->extractor) {
135
            $this->extractor = $this->getEmbed()->get($this->url);
136
        }
137
        return $this->extractor;
138
    }
139
140
    public function setCrawler(?Crawler $crawler): self
141
    {
142
        $this->crawler = $crawler;
143
        return $this;
144
    }
145
146
    public function getCrawler(): ?Crawler
147
    {
148
        return $this->crawler;
149
    }
150
}
151