Failed Conditions
Push — mediaitems ( 8f7d0e )
by Andreas
03:02
created

Display   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPreviewHtml() 0 10 2
A getIconUrl() 0 6 2
A formatDate() 0 4 1
A formatDimensions() 0 10 3
A formatFileSize() 0 4 1
1
<?php
2
3
namespace dokuwiki\Ui\Media;
4
5
use dokuwiki\Media\MediaFile;
6
7
class Display
8
{
9
10
    protected $mediaFile;
11
12
    /**
13
     * Display constructor.
14
     * @param MediaFile $mediaFile
15
     */
16
    public function __construct(MediaFile $mediaFile)
17
    {
18
        $this->mediaFile = $mediaFile;
19
    }
20
21
    /**
22
     * Get the HTML to display a preview image if possible, otherwise show an icon
23
     *
24
     * @param int $w bounding box width to resize pixel based images to
25
     * @param int $h bounding box height to resize pixel based images to
26
     * @return string
27
     */
28
    public function getPreviewHtml($w, $h)
29
    {
30
        if ($this->mediaFile->isImage()) {
31
            $src = ml($this->mediaFile, ['w' => $w, 'h' => $h]);
0 ignored issues
show
Documentation introduced by
$this->mediaFile is of type object<dokuwiki\Media\MediaFile>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
        } else {
33
            $src = $this->getIconUrl();
34
        }
35
36
        return '<img src="' . $src . '" alt="' . hsc($this->mediaFile->getDisplayName()) . '" loading="lazy" />';
37
    }
38
39
    /**
40
     * Return the URL to the icon for this file
41
     *
42
     * @return string
43
     */
44
    public function getIconUrl()
45
    {
46
        $link = 'lib/images/fileicons/svg/' . $this->mediaFile->getIcoClass() . '.svg';
47
        if (!file_exists(DOKU_INC . $link)) $link = 'lib/images/fileicons/svg/file.svg';
48
        return DOKU_BASE . $link;
49
    }
50
51
    /** @return string */
52
    protected function formatDate()
53
    {
54
        return dformat($this->mediaFile->getLastModified());
55
    }
56
57
    /**
58
     * Output the image dimension if any
59
     *
60
     * @param string $empty what to show when no dimensions are available
61
     * @return string
62
     */
63
    protected function formatDimensions($empty = '&#160')
64
    {
65
        $w = $this->mediaFile->getWidth();
66
        $h = $this->mediaFile->getHeight();
67
        if ($w && $h) {
68
            return $w . '&#215;' . $h;
69
        } else {
70
            return $empty;
71
        }
72
    }
73
74
    /** @return string */
75
    protected function formatFileSize()
76
    {
77
        return filesize_h($this->mediaFile->getFileSize());
78
    }
79
}
80