Completed
Push — master ( 9e3e67...1dbefb )
by Jeroen
08:30 queued 11s
created

ImageHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 45.16%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 105
ccs 14
cts 31
cp 0.4516
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAviaryApiKey() 0 6 1
A getName() 0 4 1
A getType() 0 4 1
A canHandle() 0 8 4
A getShowTemplate() 0 4 1
A getImageUrl() 0 4 1
A prepareMedia() 0 17 3
A __construct() 0 9 2
1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\Image;
4
5
use Kunstmaan\MediaBundle\Entity\Media;
6
use Kunstmaan\MediaBundle\Helper\ExtensionGuesserFactoryInterface;
7
use Kunstmaan\MediaBundle\Helper\File\FileHandler;
8
use Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface;
9
use Symfony\Component\HttpFoundation\File\File;
10
use Symfony\Component\Mime\MimeTypesInterface;
11
12
/**
13
 * FileHandler
14
 */
15
class ImageHandler extends FileHandler
16
{
17
    /**
18
     * @deprecated This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.
19
     */
20
    protected $aviaryApiKey;
21
22
    /**
23
     * @param int                                                $priority
24
     * @param MimeTypeGuesserFactoryInterface|MimeTypesInterface $mimeTypeGuesserFactory
0 ignored issues
show
Documentation introduced by
There is no parameter named $mimeTypeGuesserFactory. Did you maybe mean $mimeTypeGuesser?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
25
     * @param ExtensionGuesserFactoryInterface|null              $extensionGuesserFactoryInterface
0 ignored issues
show
Documentation introduced by
There is no parameter named $extensionGuesserFactoryInterface. Did you maybe mean $extensionGuesser?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
26
     * @param string                                             $aviaryApiKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $aviaryApiKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
27
     */
28 1
    public function __construct($priority, $mimeTypeGuesser, $extensionGuesser, $aviaryApiKey = null)
29
    {
30 1
        parent::__construct($priority, $mimeTypeGuesser, $extensionGuesser);
31
32 1
        if (null !== $aviaryApiKey) {
33
            @trigger_error(sprintf('Passing a value for the "$aviaryApiKey" constructor parameter of "%s" is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
34
        }
35 1
        $this->aviaryApiKey = $aviaryApiKey;
0 ignored issues
show
Deprecated Code introduced by
The property Kunstmaan\MediaBundle\He...eHandler::$aviaryApiKey has been deprecated with message: This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
36 1
    }
37
38
    /**
39
     * @deprecated This method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.
40
     *
41
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

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...
42
     */
43
    public function getAviaryApiKey()
44
    {
45
        @trigger_error(sprintf('The "%s" method is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
46
47
        return $this->aviaryApiKey;
0 ignored issues
show
Deprecated Code introduced by
The property Kunstmaan\MediaBundle\He...eHandler::$aviaryApiKey has been deprecated with message: This property is deprecated since KunstmaanMediaBundle 5.7 and will be removed in KunstmaanMediaBundle 6.0. The aviary service is discontinued.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getName()
54
    {
55
        return 'Image Handler';
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getType()
62
    {
63
        return 'image';
64
    }
65
66
    /**
67
     * @param mixed $object
68
     *
69
     * @return bool
70
     */
71
    public function canHandle($object)
72
    {
73
        if (parent::canHandle($object) && ($object instanceof File || strncmp($object->getContentType(), 'image', 5) === 0)) {
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getShowTemplate(Media $media)
84
    {
85
        return '@KunstmaanMedia/Media/Image/show.html.twig';
86
    }
87
88
    /**
89
     * @param Media  $media    The media entity
90
     * @param string $basepath The base path
91
     *
92
     * @return string
93
     */
94
    public function getImageUrl(Media $media, $basepath)
95
    {
96
        return $basepath.$media->getUrl();
97
    }
98
99
    /**
100
     * @param Media $media
101
     */
102 1
    public function prepareMedia(Media $media)
103
    {
104 1
        parent::prepareMedia($media);
105
106 1
        if ($media->getContent()) {
107 1
            $imageInfo = getimagesize($media->getContent());
108
109 1
            $width = $height = null;
110 1
            if (false !== $imageInfo) {
111
                [$width, $height] = $imageInfo;
112
            }
113
114
            $media
115 1
                ->setMetadataValue('original_width', $width)
116 1
                ->setMetadataValue('original_height', $height);
117
        }
118 1
    }
119
}
120