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

EmbedContainer::getCrawler()   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
c 1
b 0
f 0
dl 0
loc 3
rs 10
eloc 1
nc 1
nop 0
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 */
35
        $code = $this->getExtractor()->code;
36
        return $code ? ($code->width ?: 100) : 100;
37
    }
38
39
    /**
40
     * @return int
41
     */
42
    public function getHeight()
43
    {
44
        /** @var EmbedCode */
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 $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 $this->getExtractor()->url;
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
        // doesn't work?
97
        return $this->getExtractor()->type;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function validate()
104
    {
105
        return !empty($this->getExtractor()->code);
106
    }
107
108
    public function setOptions(array $options): Embeddable
109
    {
110
        $this->options = $options;
111
        return $this;
112
    }
113
114
    public function getOptions(): array
115
    {
116
        return $this->options;
117
    }
118
119
    public function setCrawler(Crawler $crawler)
120
    {
121
        $this->crawler = $crawler;
122
        return $this;
123
    }
124
125
    public function getCrawler(): Crawler
126
    {
127
        return $this->crawler;
128
    }
129
130
    public function getExtractor(): Extractor
131
    {
132
        if (!$this->extractor) {
133
            $this->extractor = $this->getEmbed()->get($this->url);
134
        }
135
        return $this->extractor;
136
    }
137
138
    private function getEmbed(): Embed
139
    {
140
        if (!$this->embed) {
141
            $this->embed = new Embed($this->getCrawler());
142
        }
143
        return $this->embed;
144
    }
145
}
146