EmbeddableExtension::EmbeddedContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LeKoala\CommonExtensions;
4
5
use Exception;
6
use Embed\Embed;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\ValidationResult;
11
12
/**
13
 * Embed external url on this data object
14
 *
15
 * @property DataObject|\LeKoala\CommonExtensions\EmbeddableExtension $owner
16
 * @property string $EmbedURL
17
 */
18
class EmbeddableExtension extends DataExtension
19
{
20
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        "EmbedURL" => "Varchar(255)"
22
    ];
23
    private static $casting = array(
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
24
        "EmbedURL" => "HTMLFragment",
25
    );
26
27
    public function updateCMSFields(FieldList $fields)
28
    {
29
        $EmbedURL = $fields->dataFieldByName('EmbedURL');
30
        if ($EmbedURL) {
31
            $EmbedURL->setTitle(_t('EmbeddableExtension.EMBEDURL', 'Embed from URL'));
32
            $EmbedURL->setDescription(_t('EmbeddableExtension.EMBEDURLDESC', 'Copy the url of a compatible ressource (eg: a Youtube video)'));
33
34
            // Position properly
35
            $Content = $fields->dataFieldByName('Content');
36
            if ($Content) {
37
                $fields->insertAfter('Content', $EmbedURL);
38
            }
39
            $Description = $fields->dataFieldByName('Description');
40
            if ($Description) {
41
                $fields->insertAfter('Description', $EmbedURL);
42
            }
43
        }
44
    }
45
46
    public function validate(ValidationResult $validationResult)
47
    {
48
        if ($this->owner->EmbedURL) {
49
            try {
50
                $embed = Embed::create($this->owner->EmbedURL);
0 ignored issues
show
Unused Code introduced by
The assignment to $embed is dead and can be removed.
Loading history...
Bug introduced by
The method create() does not exist on Embed\Embed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                /** @scrutinizer ignore-call */ 
51
                $embed = Embed::create($this->owner->EmbedURL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            } catch (Exception $ex) {
52
                $validationResult->addError($ex->getMessage());
53
            }
54
        }
55
    }
56
57
    public function EmbeddedContent()
58
    {
59
        $embed = Embed::create($this->owner->EmbedURL);
60
        $html = $embed->code;
61
        return $html;
62
    }
63
}
64