Passed
Pull Request — master (#201)
by Pascal
02:41
created

MediaOpener::getDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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