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

EmbedContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
        // 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 preg_replace('/\?.*/', '', 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, '<iframe') !== false) {
92
            return 'rich';
93
        }
94
        if (strpos($html, '<audio') !== false) {
95
            return 'audio';
96
        }
97
        if (strpos($html, '<img') !== false) {
98
            return 'photo';
99
        }
100
        return 'link';
101
        // case 'video':
102
        // case 'rich':
103
        // case 'link':
104
        // case 'photo':
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function validate()
111
    {
112
        return !empty($this->getExtractor()->code->html ?? '');
113
    }
114
115
    public function setOptions(array $options): Embeddable
116
    {
117
        $this->options = $options;
118
        return $this;
119
    }
120
121
    public function getOptions(): array
122
    {
123
        return $this->options;
124
    }
125
126
    public function getEmbed(): Embed
127
    {
128
        if (!$this->embed) {
129
            $this->embed = new Embed($this->getCrawler());
130
        }
131
        return $this->embed;
132
    }
133
134
    public function getExtractor(): Extractor
135
    {
136
        if (!$this->extractor) {
137
            $this->extractor = $this->getEmbed()->get($this->url);
138
        }
139
        return $this->extractor;
140
    }
141
142
    public function setCrawler(?Crawler $crawler): self
143
    {
144
        $this->crawler = $crawler;
145
        return $this;
146
    }
147
148
    public function getCrawler(): ?Crawler
149
    {
150
        return $this->crawler;
151
    }
152
}
153