EmbedlyBlock::getApiKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chillu\ElementalEmbedlyBlock\Block;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\SiteConfig\SiteConfig;
8
use SilverStripe\View\Requirements;
9
10
class EmbedlyBlock extends BaseElement
11
{
12
    private static $icon = 'font-icon-block-media';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
13
14
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'EmbedURL' => 'Text',
16
        'Width' => 'Varchar',
17
        'Align' => 'Enum(array("left","right","center"), "left")',
18
    ];
19
20
    private static $singular_name = 'Embed.ly';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
21
22
    private static $plural_name = 'Embed.ly';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
23
24
    private static $table_name = 'EmbedlyBlock';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    public function getType()
27
    {
28
        return _t(__CLASS__ . '.BlockType', 'Embed.ly');
29
    }
30
31
    public function getCMSFields()
32
    {
33
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
34
            $fields->dataFieldByName('EmbedURL')->setRows(1);
35
36
            $fields->dataFieldByName('Width')
37
                ->setTitle('Maximum width')
38
                ->setDescription('Example: 500px, 100%. Cards are responsive by default.');
39
        });
40
41
        return parent::getCMSFields();
42
    }
43
44
    /**
45
     * Return content summary for summary section of ElementEditor
46
     *
47
     * @return array
48
     */
49
    protected function provideBlockSchema()
50
    {
51
        $blockSchema = parent::provideBlockSchema();
52
        $blockSchema['content'] = $this->EmbedURL;
0 ignored issues
show
Bug Best Practice introduced by
The property EmbedURL does not exist on Chillu\ElementalEmbedlyBlock\Block\EmbedlyBlock. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
        return $blockSchema;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getWidthAttribute()
60
    {
61
        if (!$this->Width) {
0 ignored issues
show
Bug Best Practice introduced by
The property Width does not exist on Chillu\ElementalEmbedlyBlock\Block\EmbedlyBlock. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
            return '';
63
        }
64
65
        if (is_numeric($this->Width)) {
66
            return "{$this->Width}px";
67
        }
68
69
        // Sanitisation
70
        if (!preg_match('/[0-9](\%|px)/', $this->Width)) {
71
            return '';
72
        }
73
74
        return $this->Width;
75
    }
76
77
    public function getApiKey()
78
    {
79
        return SiteConfig::get()->First()->ElementalEmbedlyApiKey;
80
    }
81
82
}
83