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