Passed
Pull Request — master (#232)
by Pascal
03:07
created

MediaOpener::openUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 7
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
        foreach (Arr::wrap($path) as $path) {
0 ignored issues
show
introduced by
$path is overwriting one of the parameters of this function.
Loading history...
94
            $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

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

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