Completed
Pull Request — master (#2668)
by Jeroen
44:46 queued 38:10
created

PdfHandler::setWebPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\File;
4
5
use Kunstmaan\MediaBundle\Entity\Media;
6
use Kunstmaan\MediaBundle\Helper\Transformer\PreviewTransformerInterface;
7
8
/**
9
 * Custom handler for PDF files (display thumbnails if imagemagick is enabled and has PDF support)
10
 */
11
class PdfHandler extends FileHandler
12
{
13
    const TYPE = 'pdf';
14
15
    /** @var string */
16
    protected $webPath;
17
18
    /** @var PreviewTransformerInterface */
19
    protected $pdfTransformer;
20
21
    /**
22
     * Inject the root dir so we know the full path where we need to store the file.
23
     *
24
     * @param string $kernelRootDir
25
     */
26
    public function setMediaPath($kernelRootDir)
27
    {
28
        parent::setMediaPath($kernelRootDir);
29
30
        $this->setWebPath(realpath(str_replace('/', DIRECTORY_SEPARATOR, $kernelRootDir . '/../web/')) . DIRECTORY_SEPARATOR);
31
    }
32
33
    /**
34
     * @param string $webPath
35
     */
36
    public function setWebPath($webPath)
37
    {
38
        $this->webPath = $webPath;
39
    }
40
41
    /**
42
     * @param PreviewTransformerInterface $pdfTransformer
43
     */
44
    public function setPdfTransformer(PreviewTransformerInterface $pdfTransformer)
45
    {
46
        $this->pdfTransformer = $pdfTransformer;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getType()
53
    {
54
        return PdfHandler::TYPE;
55
    }
56
57
    /**
58
     * @param mixed $object
59
     *
60
     * @return bool
61
     */
62
    public function canHandle($object)
63
    {
64
        if (parent::canHandle($object) &&
65
            ($object instanceof Media && $object->getContentType() == 'application/pdf')
66
        ) {
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * @param Media $media
75
     */
76
    public function saveMedia(Media $media)
77
    {
78
        parent::saveMedia($media);
79
80
        try {
81
            // Generate preview for PDF
82
            $this->pdfTransformer->apply($this->webPath . $media->getUrl());
83
        } catch (\ImagickException $e) {
84
            // Fail silently ()
85
        }
86
    }
87
88
    /**
89
     * @param Media  $media    The media entity
90
     * @param string $basepath The base path
91
     *
92
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
93
     */
94
    public function getImageUrl(Media $media, $basepath)
95
    {
96
        $filename = $this->pdfTransformer->getPreviewFilename($basepath . $media->getUrl());
97
        if (!file_exists($this->webPath . $filename)) {
98
            return null;
99
        }
100
101
        return $filename;
102
    }
103
}
104