Passed
Push — master ( 4422d3...1f3291 )
by Pascal
19:42 queued 15:59
created

MediaOpener::fromDisk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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

139
                $mediaExporter->/** @scrutinizer ignore-call */ 
140
                                frame($this->timecode);
Loading history...
140
            }
141
        });
142
    }
143
144
    /**
145
     * Returns an instance of HLSExporter with the driver forced to AdvancedMedia.
146
     */
147
    public function exportForHLS(): HLSExporter
148
    {
149
        return new HLSExporter($this->getAdvancedDriver());
150
    }
151
152
    public function cleanupTemporaryFiles(): self
153
    {
154
        TemporaryDirectories::deleteAll();
155
156
        return $this;
157
    }
158
159
    /**
160
     * Returns the Media object from the driver.
161
     */
162
    public function __invoke(): AbstractMediaType
163
    {
164
        return $this->getDriver()->get();
165
    }
166
167
    /**
168
     * Forwards all calls to the underlying driver.
169
     * @return void
170
     */
171
    public function __call($method, $arguments)
172
    {
173
        $result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments);
174
175
        return ($result === $driver) ? $this : $result;
176
    }
177
}
178