Passed
Pull Request — 4 (#10244)
by Steve
06:01
created

EmbedContainer::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
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 Embed\EmbedCode;
8
use SilverStripe\Core\Manifest\ModuleResourceLoader;
9
10
class EmbedContainer implements Embeddable
11
{
12
    /**
13
     * @var Embed
14
     */
15
    private $embed;
16
17
    /**
18
     * @var Extractor
19
     */
20
    private $extractor;
21
22
    /**
23
     * @var string
24
     */
25
    private $url;
26
27
    /**
28
     * @var array
29
     */
30
    private $options = [];
31
32
    /**
33
     * @param string $url
34
     */
35
    public function __construct($url)
36
    {
37
        $this->url = $url;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    public function getWidth()
44
    {
45
        /** @var EmbedCode */
46
        $code = $this->getExtractor()->code;
47
        return $code ? ($code->width ?: 100) : 100;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getHeight()
54
    {
55
        /** @var EmbedCode */
56
        $code = $this->getExtractor()->code;
57
        return $code ? ($code->height ?: 100) : 100;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getPreviewURL()
64
    {
65
        $extractor = $this->getExtractor();
66
67
        // // Use thumbnail url
68
        if ($extractor->image) {
69
            return $extractor->image;
70
        }
71
72
        // Use direct image type
73
        $method = 'getApi';
74
        if (method_exists($extractor, $method)) {
75
            $api = $extractor->$method();
76
            if ($api && method_exists($api, 'url') && $api->url('direct_link')) {
77
                return $this->getExtractor()->url;
78
            }
79
        }
80
81
        // Default media
82
        return ModuleResourceLoader::resourceURL(
83
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
84
        );
85
    }
86
87
    /**
88
     * Get human readable name for this resource
89
     *
90
     * @return string
91
     */
92
    public function getName()
93
    {
94
        if ($this->getExtractor()->title) {
95
            return $this->getExtractor()->title;
96
        }
97
98
        // return preg_replace('/\?.*/', '', basename($this->getEmbed()->getUrl()));
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getType()
105
    {
106
        return $this->getExtractor()->type;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function validate()
113
    {
114
        // return !empty($this->getEmbed()->code);
115
    }
116
117
    public function setOptions(array $options): Embeddable
118
    {
119
        $this->options = $options;
120
        return $this;
121
    }
122
123
    public function getOptions(): array
124
    {
125
        return $this->options;
126
    }
127
128
    private function getExtractor(): Extractor
129
    {
130
        if (!$this->extractor) {
131
            $this->extractor = $this->getEmbed()->get($this->url);
132
        }
133
        return $this->extractor;
134
    }
135
136
    private function getEmbed(): Embed
137
    {
138
        if (!$this->embed) {
139
            $this->embed = new Embed();
140
        }
141
        return $this->embed;
142
    }
143
}
144