Passed
Push — master ( e23c5c...f972fc )
by Gabriel
03:14
created

ImageManipulator::prependCroperManipulations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Media\Manipulators\Images;
4
5
use ByTIC\MediaLibrary\Conversions\Conversion;
6
use ByTIC\MediaLibrary\Conversions\Manipulations\Manipulation;
7
use ByTIC\MediaLibrary\Conversions\Manipulations\ManipulationSequence;
8
use ByTIC\MediaLibrary\Media\Manipulators\AbstractManipulator;
9
use ByTIC\MediaLibrary\Media\Manipulators\Images\Drivers\AbstractDriver;
10
use ByTIC\MediaLibrary\Media\Manipulators\Images\Drivers\ImagineDriver;
11
use ByTIC\MediaLibrary\Media\Media;
12
use Nip\Collection;
13
14
/**
15
 * Class ImageManipulator.
16
 */
17
class ImageManipulator extends AbstractManipulator
18
{
19
    protected $driver = null;
20
21
    /**
22
     * @param Media      $media
23
     * @param Conversion $conversion
24
     */
25
    public function performConversion(Media $media, Conversion $conversion)
26
    {
27
        $manipulations = $this->prependCroperManipulations($media, $conversion->getManipulations());
28
        $imageContent = $this->getDriver()->manipulate(
29
            $media->getFile()->read(),
30
            $manipulations,
31
            $media->getExtension()
32
        );
33
34
        $path = $media->getPath($conversion->getName());
35
        $media->getCollection()->getFilesystem()->put($path, $imageContent);
36
    }
37
38
    /**
39
     * @param Media $media
40
     * @param ManipulationSequence $manipulations
41
     * @return ManipulationSequence
42
     */
43
    protected function prependCroperManipulations($media, $manipulations)
44
    {
45
        if (!isset($media->cropperData)) {
46
            return $manipulations;
47
        }
48
        $data = array_map('intval', $media->cropperData);
49
        $manipulations->prepend(Manipulation::create('crop', $data['width'], $data['height'], $data['x'], $data['y']));
50
        return $manipulations;
51
    }
52
53
    /**
54
     * @return AbstractDriver
55
     */
56 1
    public function getDriver()
57
    {
58 1
        if ($this->driver === null) {
59 1
            $this->driver = $this->newDriver();
60
        }
61
62 1
        return $this->driver;
63
    }
64
65
    /**
66
     * @return ImagineDriver
67
     */
68 1
    protected function newDriver()
69
    {
70 1
        return new ImagineDriver();
71
    }
72
73
    /**
74
     * @return bool
75
     */
76 4
    public function requirementsAreInstalled(): bool
77
    {
78 4
        return true;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4
    public function supportedExtensions(): Collection
85
    {
86 4
        return new Collection(['png', 'jpg', 'jpeg', 'gif']);
0 ignored issues
show
Deprecated Code introduced by
The class Nip\Collection has been deprecated: Use new Collection class from Nip/Collection repo ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
        return /** @scrutinizer ignore-deprecated */ new Collection(['png', 'jpg', 'jpeg', 'gif']);
Loading history...
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function supportedMimeTypes(): Collection
93
    {
94
        return new Collection(['image/jpeg', 'image/gif', 'image/png']);
0 ignored issues
show
Deprecated Code introduced by
The class Nip\Collection has been deprecated: Use new Collection class from Nip/Collection repo ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

94
        return /** @scrutinizer ignore-deprecated */ new Collection(['image/jpeg', 'image/gif', 'image/png']);
Loading history...
95
    }
96
}
97