Passed
Pull Request — master (#26)
by Robbie
02:07
created

FileBlock::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\Block;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Core\Manifest\ModuleResourceLoader;
8
9
class FileBlock extends BaseElement
10
{
11
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'File' => File::class,
13
    ];
14
15
    private static $owns = [
0 ignored issues
show
Unused Code introduced by
The property $owns is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'File',
17
    ];
18
19
    private static $singular_name = 'file';
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    private static $plural_name = 'files';
0 ignored issues
show
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    private static $icon = 'font-icon-block-file';
0 ignored issues
show
Unused Code introduced by
The property $icon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
24
25
    public function getType()
26
    {
27
        return _t(__CLASS__ . '.BlockType', 'File');
28
    }
29
30
    public function getSummary()
31
    {
32
        if ($this->File() && $this->File()->exists()) {
0 ignored issues
show
Bug introduced by
The method File() does not exist on SilverStripe\ElementalBlocks\Block\FileBlock. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
33
            return $this->getSummaryThumbnail() . $this->File()->Title;
34
        }
35
        return '';
36
    }
37
38
    /**
39
     * Return a thumbnail of the file, if it's an image. Used in GridField preview summaries.
40
     *
41
     * @return string|null
42
     */
43
    public function getSummaryThumbnail()
44
    {
45
        if ($this->File() && $this->File()->exists()) {
46
            $template = '<span class="elemental-preview__thumbnail-image">%s</span>';
47
48
            if ($this->File()->getIsImage()) {
49
                if ($this->File()->getHeight() > $this->File()->getWidth()) {
50
                    // portrait
51
                    $image = $this->File()->ScaleWidth(36)->CropHeight(36);
52
                } else {
53
                    // landscape
54
                    $image = $this->File()->ScaleHeight(36)->CropWidth(36);
55
                }
56
57
                return sprintf($template, $image);
58
            }
59
60
            $placeholderFilename = ModuleResourceLoader::resourceURL(
61
                'silverstripe/framework:client/images/app_icons/document_92.png'
62
            );
63
64
            return sprintf(
65
                $template,
66
                '<img alt="' . _t(__CLASS__ . '.BlockType', 'File') . '" src="' . $placeholderFilename . '"'
67
                . ' class="elemental-preview__thumbnail-image--placeholder" />'
68
            );
69
        }
70
    }
71
}
72