Completed
Push — master ( 169c44...f098b1 )
by
unknown
12s
created

FileBlock::getSummaryThumbnail()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 0
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\Block;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image_Backend;
8
use SilverStripe\Core\Manifest\ModuleResourceLoader;
9
use SilverStripe\ORM\FieldType\DBHTMLText;
10
11
class FileBlock extends BaseElement
12
{
13
    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...
14
        'File' => File::class,
15
    ];
16
17
    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...
18
        'File',
19
    ];
20
21
    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...
22
23
    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...
24
25
    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...
26
27
    public function getType()
28
    {
29
        return _t(__CLASS__ . '.BlockType', 'File');
30
    }
31
32
    public function getSummary()
33
    {
34
        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

34
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
35
            return $this->getSummaryThumbnail() . $this->File()->Title;
36
        }
37
        return '';
38
    }
39
40
    /**
41
     * Return a thumbnail of the file, if it's an image. Used in GridField preview summaries.
42
     *
43
     * @return DBHTMLText
44
     */
45
    public function getSummaryThumbnail()
46
    {
47
        $data = [];
48
49
        if ($this->File() && $this->File()->exists()) {
50
            if ($this->File()->getIsImage()) {
51
                // Stretch to maximum of 36px either way then trim the extra off
52
                if ($this->File()->getOrientation() === Image_Backend::ORIENTATION_PORTRAIT) {
53
                    $data['Image'] = $this->File()->ScaleWidth(36)->CropHeight(36);
54
                } else {
55
                    $data['Image'] = $this->File()->ScaleHeight(36)->CropWidth(36);
56
                }
57
            } else {
58
                $data = [
59
                    'Image' => ModuleResourceLoader::resourceURL(
60
                        'silverstripe/framework:client/images/app_icons/document_92.png'
61
                    ),
62
                    'IsPlaceholder' => true
63
                ];
64
            }
65
        }
66
67
        return $this->customise($data)->renderWith(__CLASS__ . '/SummaryThumbnail');
68
    }
69
}
70