Completed
Push — master ( 204e09...86e349 )
by Freek
04:41 queued 02:13
created

Conversion::nonQueued()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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