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

EmbedContainer::getPreviewURL()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 21
rs 9.2222
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
            return (string) $extractor->image;
59
        }
60
61
        // Use direct image type
62
        $method = 'getApi';
63
        if (method_exists($extractor, $method)) {
64
            $api = $extractor->$method();
65
            if ($api && method_exists($api, 'url') && $api->url('direct_link')) {
66
                return $api->url('direct_link'); // <<< TODO test this works
67
            }
68
        }
69
70
        // Default media
71
        return ModuleResourceLoader::resourceURL(
72
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
73
        );
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getName()
80
    {
81
        $extractor = $this->getExtractor();
82
        if ($extractor->title) {
83
            return $extractor->title;
84
        }
85
        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...
86
            return preg_replace('/\?.*/', '', basename($extractor->url->getPath()));
87
        }
88
        return '';
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getType()
95
    {
96
        $html = $this->getExtractor()->code->html ?? '';
97
        if (strpos($html, '<video') !== false) {
98
            return 'video';
99
        }
100
        if (strpos($html, '<iframe') !== false) {
101
            return 'rich';
102
        }
103
        if (strpos($html, '<audio') !== false) {
104
            return 'audio';
105
        }
106
        if (strpos($html, '<img') !== false) {
107
            return 'photo';
108
        }
109
        return 'link';
110
        // case 'video':
111
        // case 'rich':
112
        // case 'link':
113
        // case 'photo':
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function validate()
120
    {
121
        return !empty($this->getExtractor()->code->html ?? '');
122
    }
123
124
    public function setOptions(array $options): Embeddable
125
    {
126
        $this->options = $options;
127
        return $this;
128
    }
129
130
    public function getOptions(): array
131
    {
132
        return $this->options;
133
    }
134
135
    public function getEmbed(): Embed
136
    {
137
        if (!$this->embed) {
138
            $this->embed = new Embed($this->getCrawler());
139
        }
140
        return $this->embed;
141
    }
142
143
    public function getExtractor(): Extractor
144
    {
145
        if (!$this->extractor) {
146
            $this->extractor = $this->getEmbed()->get($this->url);
147
        }
148
        return $this->extractor;
149
    }
150
151
    public function setCrawler(?Crawler $crawler): self
152
    {
153
        $this->crawler = $crawler;
154
        return $this;
155
    }
156
157
    public function getCrawler(): ?Crawler
158
    {
159
        return $this->crawler;
160
    }
161
}
162