Completed
Push — master ( 06c49a...d653f4 )
by Rob
02:07
created

logMetadataReaderReplaced()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * By default, a metadata reader based on the exif php extension is used.
19
 * This compiler pass checks if the extension is loaded an switches to a simpler
20
 * implementation if not.
21
 */
22
class MetadataReaderCompilerPass implements CompilerPassInterface
23
{
24
    const METADATA_READER_PARAM = 'liip_imagine.meta_data.reader.class';
25
26
    const DEFAULT_METADATA_READER_CLASS = 'Imagine\Image\Metadata\DefaultMetadataReader';
27
28
    const EXIF_METADATA_READER_CLASS = 'Imagine\Image\Metadata\ExifMetadataReader';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function process(ContainerBuilder $container)
34
    {
35
        if (!$this->extExifIsAvailable() && $this->isDefaultMetadataReader($container)) {
36
            $container->setParameter(self::METADATA_READER_PARAM, self::DEFAULT_METADATA_READER_CLASS);
37
            $this->logMetadataReaderReplaced($container);
38
        }
39
    }
40
41
    /**
42
     * @param ContainerBuilder $container
43
     *
44
     * @return bool
45
     */
46
    protected function isDefaultMetadataReader(ContainerBuilder $container)
47
    {
48
        $currentMetadataReaderParameter = $container->getParameter(self::METADATA_READER_PARAM);
49
50
        return $currentMetadataReaderParameter === self::EXIF_METADATA_READER_CLASS;
51
    }
52
53
    /**
54
     * @param ContainerBuilder $container
55
     */
56
    protected function logMetadataReaderReplaced(ContainerBuilder $container)
57
    {
58
        $compiler = $container->getCompiler();
59
        $formatter = $compiler->getLoggingFormatter();
60
        $message = 'Automatically replaced Imagine ExifMetadataReader with DefaultMetadataReader; '.
61
            'you might experience issues with LiipImagineBundle; reason: PHP extension "exif" is missing; solution: '.
62
            'for advanced metadata extraction install the PHP extension "exif" or set a custom MetadataReader '.
63
            'through the "liip_imagine.meta_data.reader.class" parameter';
64
65
        $compiler->addLogMessage($formatter->format($this, $message));
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    protected function extExifIsAvailable()
72
    {
73
        return extension_loaded('exif');
74
    }
75
}
76