Completed
Push — l10n_master ( 453465...12070f )
by Jeroen
15:21 queued 08:31
created

PdfTransformer::getPreviewFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\Transformer;
4
5
class PdfTransformer implements PreviewTransformerInterface
6
{
7
    /** @var \Imagick */
8
    protected $imagick;
9
10 3
    public function __construct(\Imagick $imagick)
11
    {
12 3
        $this->imagick = $imagick;
13 3
    }
14
15
    /**
16
     * Apply the transformer on the absolute path and return an altered version of it.
17
     *
18
     * @param string $absolutePath
19
     *
20
     * @return string|false
21
     */
22 2
    public function apply($absolutePath)
23
    {
24 2
        $info = pathinfo($absolutePath);
25
26 2
        if (isset($info['extension']) && false !== strpos(strtolower($info['extension']), 'pdf') && file_exists($absolutePath)) {
27
            // If it doesn't exist yet, extract the first page of the PDF
28 2
            $previewFilename = $this->getPreviewFilename($absolutePath);
29 2
            if (!file_exists($previewFilename)) {
30 1
                $this->imagick->readImage($absolutePath . '[0]');
31 1
                $this->imagick->setImageFormat('jpg');
32 1
                $this->imagick->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
33 1
                $this->imagick->writeImage($previewFilename);
34 1
                $this->imagick->clear();
35
            }
36
37 2
            $absolutePath = $previewFilename;
38
        }
39
40 2
        return $absolutePath;
41
    }
42
43
    /**
44
     * @param string $absolutePath
45
     *
46
     * @return string
47
     */
48 3
    public function getPreviewFilename($absolutePath)
49
    {
50 3
        return $absolutePath . '.jpg';
51
    }
52
}
53