Passed
Push — master ( 16e851...25995c )
by Daniel
09:34
created

EmbedResource::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\View\Embed;
4
5
use Embed\Adapters\Adapter;
6
use Embed\Embed;
7
use Embed\Http\DispatcherInterface;
8
use SilverStripe\Core\Manifest\ModuleResourceLoader;
9
10
/**
11
 * Encapsulation of an embed tag, linking to an external media source.
12
 *
13
 * @see Embed
14
 */
15
class EmbedResource implements Embeddable
16
{
17
    /**
18
     * Embed result
19
     *
20
     * @var Adapter
21
     */
22
    protected $embed;
23
24
    /**
25
     * @var string
26
     */
27
    protected $url;
28
29
    /**
30
     * @var array
31
     */
32
    protected $options;
33
34
    /**
35
     * @var DispatcherInterface
36
     */
37
    protected $dispatcher;
38
39
    /**
40
     * @param string @url
0 ignored issues
show
Documentation Bug introduced by
The doc comment @url at position 0 could not be parsed: Unknown type name '@url' at position 0 in @url.
Loading history...
41
     */
42
    public function __construct($url)
43
    {
44
        $this->url = $url;
45
    }
46
47
    public function getWidth()
48
    {
49
        return $this->getEmbed()->getWidth() ?: 100;
50
    }
51
52
    public function getHeight()
53
    {
54
        return $this->getEmbed()->getHeight() ?: 100;
55
    }
56
57
    public function getPreviewURL()
58
    {
59
        // Use thumbnail url
60
        if ($this->getEmbed()->image) {
61
            return $this->getEmbed()->image;
62
        }
63
64
        // Use direct image type
65
        if ($this->getType() === 'photo' && !empty($this->getEmbed()->url)) {
66
            return $this->getEmbed()->url;
67
        }
68
69
        // Default media
70
        return ModuleResourceLoader::resourceURL(
71
            'silverstripe/asset-admin:client/dist/images/icon_file.png'
72
        );
73
    }
74
75
    /**
76
     * Get human readable name for this resource
77
     *
78
     * @return string
79
     */
80
    public function getName()
81
    {
82
        if ($this->getEmbed()->title) {
83
            return $this->getEmbed()->title;
84
        }
85
86
        return preg_replace('/\?.*/', '', basename($this->getEmbed()->getUrl()));
87
    }
88
89
    public function getType()
90
    {
91
        return $this->getEmbed()->type;
92
    }
93
94
    public function validate()
95
    {
96
        return !empty($this->getEmbed()->code);
97
    }
98
99
    /**
100
     * @param array $options
101
     * @return $this
102
     */
103
    public function setOptions(array $options)
104
    {
105
        $this->options = $options;
106
        return $this;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getOptions()
113
    {
114
        return $this->options;
115
    }
116
117
    /**
118
     * @param DispatcherInterface $dispatcher
119
     * @return $this
120
     */
121
    public function setDispatcher(DispatcherInterface $dispatcher)
122
    {
123
        $this->dispatcher = $dispatcher;
124
        return $this;
125
    }
126
127
    /**
128
     * @return DispatcherInterface
129
     */
130
    public function getDispatcher()
131
    {
132
        return $this->dispatcher;
133
    }
134
135
    /**
136
     * Returns a bootstrapped Embed object
137
     *
138
     * @return Adapter
139
     */
140
    public function getEmbed()
141
    {
142
        if (!$this->embed) {
143
            $this->embed = Embed::create($this->url, $this->getOptions(), $this->getDispatcher());
144
        }
145
        return $this->embed;
146
    }
147
}
148