Passed
Pull Request — 4 (#10244)
by Steve
08:00
created

EmbedContainer::getWidth()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace SilverStripe\View\Embed;
4
5
use Embed\Extractor;
6
use Embed\Embed;
7
use Psr\Http\Message\UriInterface;
8
use SilverStripe\Core\Injector\Injectable;
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
    use Injectable;
17
18
    private static $dependencies = [
0 ignored issues
show
introduced by
The private property $dependencies is not used, and could be removed.
Loading history...
19
        'embed' => '%$' . Embed::class,
20
    ];
21
22
    public Embed $embed;
23
24
    private ?Extractor $extractor = null;
25
26
    private string $url;
27
28
    private array $options = [];
29
30
    public function __construct(string $url)
31
    {
32
        $this->url = $url;
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    public function getWidth()
39
    {
40
        $code = $this->getExtractor()->code;
41
        return $code ? ($code->width ?: 100) : 100;
42
    }
43
44
    /**
45
     * @return int
46
     */
47
    public function getHeight()
48
    {
49
        $code = $this->getExtractor()->code;
50
        return $code ? ($code->height ?: 100) : 100;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getPreviewURL()
57
    {
58
        $extractor = $this->getExtractor();
59
60
        // Use thumbnail url
61
        if ($extractor->image) {
62
            return (string) $extractor->image;
63
        }
64
65
        // Default media
66
        return ModuleResourceLoader::resourceURL(
67
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
68
        );
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getName()
75
    {
76
        $extractor = $this->getExtractor();
77
        if ($extractor->title) {
78
            return $extractor->title;
79
        }
80
        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...
81
            return basename($extractor->url->getPath());
82
        }
83
        return '';
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getType()
90
    {
91
        $html = $this->getExtractor()->code->html ?? '';
92
        if (strpos($html, '<video') !== false) {
93
            return 'video';
94
        }
95
        if (strpos($html, '<audio') !== false) {
96
            return 'audio';
97
        }
98
        foreach (['iframe', 'blockquote', 'pre', 'script', 'style'] as $richTag) {
99
            if (strpos($html, "<{$richTag}") !== false) {
100
                return 'rich';
101
            }
102
        }
103
        if (strpos($html, '<img') !== false) {
104
            return 'photo';
105
        }
106
        return 'link';
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function validate()
113
    {
114
        return !empty($this->getExtractor()->code->html ?? '');
115
    }
116
117
    public function getOptions(): array
118
    {
119
        return $this->options;
120
    }
121
122
    public function setOptions(array $options): self
123
    {
124
        $this->options = $options;
125
        return $this;
126
    }
127
128
    /**
129
     * Calling this method will trigger the HTTP call(s) to the remote url
130
     */
131
    public function getExtractor(): Extractor
132
    {
133
        if (!$this->extractor) {
134
            $this->extractor = $this->embed->get($this->url);
135
        }
136
        return $this->extractor;
137
    }
138
}
139