Completed
Push — master ( b8da20...856b73 )
by
unknown
02:45
created

EmbedResource::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Model;
4
5
use Embed\Adapters\Adapter;
6
use Embed\Embed;
7
use SilverStripe\Core\Manifest\ModuleLoader;
8
9
/**
10
 * Encapsulation of an embed tag, linking to an external media source.
11
 *
12
 * @see Embed
13
 */
14
class EmbedResource
15
{
16
    /**
17
     * Embed result
18
     *
19
     * @var Adapter
20
     */
21
    protected $embed;
22
23
    public function __construct($url)
24
    {
25
        $this->embed = Embed::create($url);
26
    }
27
28
29
    /**
30
     * Get width of this Embed
31
     *
32
     * @return int
33
     */
34
    public function getWidth()
35
    {
36
        return $this->embed->getWidth() ?: 100;
37
    }
38
39
    /**
40
     * Get height of this Embed
41
     *
42
     * @return int
43
     */
44
    public function getHeight()
45
    {
46
        return $this->embed->getHeight() ?: 100;
47
    }
48
49
    public function getPreviewURL()
50
    {
51
        // Use thumbnail url
52
        if ($this->embed->image) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->embed->image of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
            return $this->embed->image;
54
        }
55
56
        // Use direct image type
57
        if ($this->getType() === 'photo' && !empty($this->embed->url)) {
58
            return $this->embed->url;
59
        }
60
61
        // Default media
62
        return ModuleLoader::getModule('silverstripe/admin')
63
            ->getResourcePath('client/dist/images/src/default_media.png');
64
    }
65
66
    /**
67
     * Get human readable name for this resource
68
     *
69
     * @return string
70
     */
71
    public function getName()
72
    {
73
        if ($this->embed->title) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->embed->title of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74
            return $this->embed->title;
75
        }
76
77
        return preg_replace('/\?.*/', '', basename($this->embed->getUrl()));
78
    }
79
80
    /**
81
     * Get Embed type
82
     *
83
     * @return string
84
     */
85
    public function getType()
86
    {
87
        return $this->embed->type;
88
    }
89
}
90