Completed
Push — master ( d88e75...b283ff )
by Freek
02:10
created

Conversion::shouldKeepOriginalImageFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Spatie\MediaLibrary\Conversion;
4
5
use BadMethodCallException;
6
use Spatie\Image\Manipulations;
7
8
/** @mixin \Spatie\Image\Manipulations */
9
class Conversion
10
{
11
    /** @var string */
12
    protected $name = '';
13
14
    /** @var int */
15
    protected $extractVideoFrameAtSecond = 0;
16
17
    /** @var \Spatie\Image\Manipulations */
18
    protected $manipulations;
19
20
    /** @var array */
21
    protected $performOnCollections = [];
22
23
    /** @var bool */
24
    protected $performOnQueue = true;
25
26
    /** @var bool */
27
    protected $keepOriginalImageFormat = false;
28
29
    public function __construct(string $name)
30
    {
31
        $this->name = $name;
32
33
        $this->manipulations = (new Manipulations())->format('jpg');
34
    }
35
36
    public static function create(string $name)
37
    {
38
        return new static($name);
39
    }
40
41
    public function getName(): string
42
    {
43
        return $this->name;
44
    }
45
46
    /*
47
     * Set the timecode in seconds to extract a video thumbnail.
48
     * Only used on video media.
49
     */
50
    public function extractVideoFrameAtSecond(int $timecode): Conversion
51
    {
52
        $this->extractVideoFrameAtSecond = $timecode;
53
54
        return $this;
55
    }
56
57
    public function getExtractVideoFrameAtSecond(): int
58
    {
59
        return $this->extractVideoFrameAtSecond;
60
    }
61
62
    public function keepOriginalImageFormat(): Conversion
63
    {
64
        $this->keepOriginalImageFormat = true;
65
66
        return $this;
67
    }
68
69
    public function shouldKeepOriginalImageFormat(): Bool
70
    {
71
        return $this->keepOriginalImageFormat;
72
    }
73
74
    public function getManipulations(): Manipulations
75
    {
76
        return $this->manipulations;
77
    }
78
79
    public function removeManipulation(string $manipulationName)
80
    {
81
        $this->manipulations->removeManipulation($manipulationName);
82
83
        return $this;
84
    }
85
86
    public function __call($name, $arguments)
87
    {
88
        if (! method_exists($this->manipulations, $name)) {
89
            throw new BadMethodCallException("Manipulation `{$name}` does not exist");
90
        }
91
92
        $this->manipulations->$name(...$arguments);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Set the manipulations for this conversion.
99
     *
100
     * @param \Spatie\Image\Manipulations|closure $manipulations
101
     *
102
     * @return $this
103
     */
104
    public function setManipulations($manipulations)
105
    {
106
        if ($manipulations instanceof Manipulations) {
107
            $this->manipulations = $this->manipulations->mergeManipulations($manipulations);
108
        }
109
110
        if (is_callable($manipulations)) {
111
            $manipulations($this->manipulations);
112
        }
113
114
        return $this;
115
    }
116
117
    /**
118
     * Add the given manipulations as the first ones.
119
     *
120
     * @param \Spatie\Image\Manipulations $manipulations
121
     *
122
     * @return $this
123
     */
124
    public function addAsFirstManipulations(Manipulations $manipulations)
125
    {
126
        $manipulationSequence = $manipulations->getManipulationSequence()->toArray();
127
128
        $this->manipulations
129
            ->getManipulationSequence()
130
            ->mergeArray($manipulationSequence);
131
132
        return $this;
133
    }
134
135
    /**
136
     * Set the collection names on which this conversion must be performed.
137
     *
138
     * @param  $collectionNames
139
     *
140
     * @return $this
141
     */
142
    public function performOnCollections(...$collectionNames)
143
    {
144
        $this->performOnCollections = $collectionNames;
145
146
        return $this;
147
    }
148
149
    /*
150
     * Determine if this conversion should be performed on the given
151
     * collection.
152
     */
153
    public function shouldBePerformedOn(string $collectionName): bool
154
    {
155
        //if no collections were specified, perform conversion on all collections
156
        if (! count($this->performOnCollections)) {
157
            return true;
158
        }
159
160
        if (in_array('*', $this->performOnCollections)) {
161
            return true;
162
        }
163
164
        return in_array($collectionName, $this->performOnCollections);
165
    }
166
167
    /**
168
     * Mark this conversion as one that should be queued.
169
     *
170
     * @return $this
171
     */
172
    public function queued()
173
    {
174
        $this->performOnQueue = true;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Mark this conversion as one that should not be queued.
181
     *
182
     * @return $this
183
     */
184
    public function nonQueued()
185
    {
186
        $this->performOnQueue = false;
187
188
        return $this;
189
    }
190
191
    /*
192
     * Determine if the conversion should be queued.
193
     */
194
    public function shouldBeQueued(): bool
195
    {
196
        return $this->performOnQueue;
197
    }
198
199
    /*
200
     * Get the extension that the result of this conversion must have.
201
     */
202
    public function getResultExtension(string $originalFileExtension = ''): string
203
    {
204
        if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
205
            return $manipulationArgument;
206
        }
207
208
        return $originalFileExtension;
209
    }
210
}
211