Passed
Pull Request — master (#232)
by Pascal
02:29
created

MediaOpener::openUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg;
4
5
use FFMpeg\Coordinate\TimeCode;
6
use FFMpeg\Media\AbstractMediaType;
7
use Illuminate\Contracts\Filesystem\Filesystem;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Traits\ForwardsCalls;
11
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
12
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSExporter;
13
use ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter;
14
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk;
15
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
16
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection;
17
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaOnNetwork;
18
use ProtoneMedia\LaravelFFMpeg\Filesystem\TemporaryDirectories;
19
20
/**
21
 * @mixin \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
22
 */
23
class MediaOpener
24
{
25
    use ForwardsCalls;
26
27
    /**
28
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
29
     */
30
    private $disk;
31
32
    /**
33
     * @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
34
     */
35
    private $driver;
36
37
    /**
38
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection
39
     */
40
    private $collection;
41
42
    /**
43
     * @var \FFMpeg\Coordinate\TimeCode
44
     */
45
    private $timecode;
46
47
    /**
48
     * Uses the 'filesystems.default' disk from the config if none is given.
49
     * Gets the underlying PHPFFMpeg instance from the container if none is given.
50
     * Instantiates a fresh MediaCollection if none is given.
51
     */
52
    public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollection $mediaCollection = null)
53
    {
54
        $this->disk = Disk::make($disk ?: config('filesystems.default'));
55
56
        $this->driver = ($driver ?: app(PHPFFMpeg::class))->fresh();
57
58
        $this->collection = $mediaCollection ?: new MediaCollection;
59
    }
60
61
    public function clone(): self
62
    {
63
        return new MediaOpener(
64
            $this->disk,
65
            $this->driver,
66
            $this->collection
67
        );
68
    }
69
70
    /**
71
     * Set the disk to open files from.
72
     */
73
    public function fromDisk($disk): self
74
    {
75
        $this->disk = Disk::make($disk);
76
77
        return $this;
78
    }
79
80
    /**
81
     * Alias for 'fromDisk', mostly for backwards compatibility.
82
     */
83
    public function fromFilesystem(Filesystem $filesystem): self
84
    {
85
        return $this->fromDisk($filesystem);
86
    }
87
88
    /**
89
     * Instantiates a Media object for each given path.
90
     */
91
    public function open($path): self
92
    {
93
        $paths = Arr::wrap($path);
94
95
        foreach ($paths as $path) {
0 ignored issues
show
introduced by
$path is overwriting one of the parameters of this function.
Loading history...
96
            $this->collection->push(Media::make($this->disk, $path));
0 ignored issues
show
Bug introduced by
The method push() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

96
            $this->collection->/** @scrutinizer ignore-call */ 
97
                               push(Media::make($this->disk, $path));
Loading history...
97
        }
98
99
        return $this;
100
    }
101
102
    public function openUrl($path, array $headers = []): self
103
    {
104
        $paths = Arr::wrap($path);
105
106
        foreach ($paths as $path) {
0 ignored issues
show
introduced by
$path is overwriting one of the parameters of this function.
Loading history...
107
            $this->collection->push(MediaOnNetwork::make($path, $headers));
108
        }
109
110
        return $this;
111
    }
112
113
    public function get(): MediaCollection
114
    {
115
        return $this->collection;
116
    }
117
118
    public function getDriver(): PHPFFMpeg
119
    {
120
        return $this->driver->open($this->collection);
121
    }
122
123
    /**
124
     * Forces the driver to open the collection with the `openAdvanced` method.
125
     */
126
    public function getAdvancedDriver(): PHPFFMpeg
127
    {
128
        return $this->driver->openAdvanced($this->collection);
129
    }
130
131
    /**
132
     * Shortcut to set the timecode by string.
133
     */
134
    public function getFrameFromString(string $timecode): self
135
    {
136
        return $this->getFrameFromTimecode(TimeCode::fromString($timecode));
137
    }
138
139
    /**
140
     * Shortcut to set the timecode by seconds.
141
     */
142
    public function getFrameFromSeconds(float $quantity): self
143
    {
144
        return $this->getFrameFromTimecode(TimeCode::fromSeconds($quantity));
145
    }
146
147
    public function getFrameFromTimecode(TimeCode $timecode): self
148
    {
149
        $this->timecode = $timecode;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Returns an instance of MediaExporter with the driver and timecode (if set).
156
     */
157
    public function export(): MediaExporter
158
    {
159
        return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) {
160
            if ($this->timecode) {
161
                $mediaExporter->frame($this->timecode);
0 ignored issues
show
Bug introduced by
The method frame() does not exist on ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

161
                $mediaExporter->/** @scrutinizer ignore-call */ 
162
                                frame($this->timecode);
Loading history...
162
            }
163
        });
164
    }
165
166
    /**
167
     * Returns an instance of HLSExporter with the driver forced to AdvancedMedia.
168
     */
169
    public function exportForHLS(): HLSExporter
170
    {
171
        return new HLSExporter($this->getAdvancedDriver());
172
    }
173
174
    public function cleanupTemporaryFiles(): self
175
    {
176
        TemporaryDirectories::deleteAll();
177
178
        return $this;
179
    }
180
181
    public function each($items, callable $callback): self
182
    {
183
        Collection::make($items)->each(function ($item, $key) use ($callback) {
184
            return $callback($this->clone(), $item, $key);
185
        });
186
187
        return $this;
188
    }
189
190
    /**
191
     * Returns the Media object from the driver.
192
     */
193
    public function __invoke(): AbstractMediaType
194
    {
195
        return $this->getDriver()->get();
196
    }
197
198
    /**
199
     * Forwards all calls to the underlying driver.
200
     * @return void
201
     */
202
    public function __call($method, $arguments)
203
    {
204
        $result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments);
205
206
        return ($result === $driver) ? $this : $result;
207
    }
208
}
209