FileRenderer::isImage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace rtens\domin\delivery\web\renderers;
3
4
use rtens\domin\delivery\Renderer;
5
use rtens\domin\parameters\File;
6
use rtens\domin\delivery\web\Element;
7
8
class FileRenderer implements Renderer {
9
10
    /**
11
     * @param mixed $value
12
     * @return bool
13
     */
14
    public function handles($value) {
15
        return $value instanceof File;
16
    }
17
18
    /**
19
     * @param File $value
20
     * @return string
21
     */
22
    public function render($value) {
23
        if ($this->isImage($value)) {
24
            return (string)new Element('img', [
25
                'title' => $value->getName(),
26
                'src' => $this->createUrl($value),
27
                'style' => 'max-height:' . $this->maxHeight(),
28
            ]);
29
        }
30
31
        return (string)new Element('a', [
32
            'download' => $value->getName(),
33
            'href' => $this->createUrl($value),
34
            'target' => '_blank'
35
        ], [
36
            $value->getName()
37
        ]);
38
    }
39
40
    protected function createUrl(File $file) {
41
        return 'data:' . $file->getType() . ';base64,' . base64_encode($file->getContent());
42
    }
43
44
    protected function isImage(File $file) {
45
        return strpos($file->getType(), 'image') === 0;
46
    }
47
48
    private function maxHeight() {
49
        return '10em';
50
    }
51
}