FileBlock::getSummary()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 9.4285
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
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
14
        'File' => File::class,
15
    ];
16
17
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
18
        'File',
19
    ];
20
21
    private static $singular_name = 'file';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
22
23
    private static $plural_name = 'files';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
24
25
    private static $icon = 'font-icon-block-file';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
26
27
    private static $table_name = 'S_EB_FileBlock';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
28
29
    public function getType()
30
    {
31
        return _t(__CLASS__ . '.BlockType', 'File');
32
    }
33
34
    public function getSummary()
35
    {
36
        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

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