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

PdfTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 20 5
A getPreviewFilename() 0 4 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