Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

MediaBundle/Helper/File/SVGMimeTypeGuesser.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\File;
4
5
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
6
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
7
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
8
9
/**
10
 * SVGMimeTypeGuesser
11
 *
12
 * Simple Mime type guesser to detect SVG image files, it will test if the file is an XML file and return SVG mime type
13
 * if the XML contains a valid SVG namespace...
14
 */
15
class SVGMimeTypeGuesser implements MimeTypeGuesserInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Symfony\Component\HttpFo...imeTypeGuesserInterface has been deprecated with message: since Symfony 4.3, use {@link MimeTypesInterface} instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    private $_MIMETYPE_NAMESPACES = array(
18
        'http://www.w3.org/2000/svg' => 'image/svg+xml',
19
    );
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function guess($path)
25
    {
26
        if (!is_file($path)) {
27
            throw new FileNotFoundException($path);
28
        }
29
30
        if (!is_readable($path)) {
31
            throw new AccessDeniedException($path);
32
        }
33
34
        if (!self::isSupported()) {
35
            return;
36
        }
37
38
        $dom = new \DOMDocument();
39
        $xml = $dom->load($path, LIBXML_NOERROR + LIBXML_ERR_FATAL + LIBXML_ERR_NONE);
40
        if ($xml === false) {
41
            return;
42
        }
43
        $xpath = new \DOMXPath($dom);
44
        foreach ($xpath->query('namespace::*') as $node) {
45
            if (isset($this->_MIMETYPE_NAMESPACES[$node->nodeValue])) {
46
                return $this->_MIMETYPE_NAMESPACES[$node->nodeValue];
47
            }
48
        }
49
50
        return;
51
    }
52
53
    /**
54
     * Returns whether this guesser is supported on the current OS
55
     *
56
     * @return bool
57
     */
58
    public static function isSupported()
59
    {
60
        return class_exists('DOMDocument') && class_exists('DOMXPath');
61
    }
62
}
63