Completed
Branch v7 (f01542)
by Pascal
08:46
created

MediaOpener   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 21
eloc 34
c 2
b 0
f 0
dl 0
loc 108
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A open() 0 9 2
A getDriver() 0 3 1
A get() 0 3 1
A getFrameFromTimecode() 0 5 1
A exportForHLS() 0 3 1
A export() 0 5 2
A __invoke() 0 3 1
A fromFilesystem() 0 3 1
A fromDisk() 0 5 1
A __call() 0 5 2
A getFrameFromString() 0 4 1
A getFrameFromSeconds() 0 4 1
A getAdvancedDriver() 0 3 1
A cleanupTemporaryFiles() 0 5 1
A __construct() 0 7 4
1
<?php
2
3
namespace Pbmedia\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 Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
11
use Pbmedia\LaravelFFMpeg\Exporters\HLSExporter;
12
use Pbmedia\LaravelFFMpeg\Exporters\MediaExporter;
13
use Pbmedia\LaravelFFMpeg\Filesystem\Disk;
14
use Pbmedia\LaravelFFMpeg\Filesystem\Media;
15
use Pbmedia\LaravelFFMpeg\Filesystem\MediaCollection;
16
use Pbmedia\LaravelFFMpeg\Filesystem\TemporaryDirectories;
17
18
class MediaOpener
19
{
20
    use ForwardsCalls;
21
22
    private Disk $disk;
23
    private PHPFFMpeg $driver;
24
    private MediaCollection $collection;
25
    private ?TimeCode $timecode = null;
26
27
    public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollection $mediaCollection = null)
28
    {
29
        $this->disk = Disk::make($disk ?: config('filesystems.default'));
30
31
        $this->driver = $driver ?: app(PHPFFMpeg::class);
32
33
        $this->collection = $mediaCollection ?: new MediaCollection;
34
    }
35
36
    public function fromDisk($disk): self
37
    {
38
        $this->disk = Disk::make($disk);
39
40
        return $this;
41
    }
42
43
    public function fromFilesystem(Filesystem $filesystem): self
44
    {
45
        return $this->fromDisk($filesystem);
46
    }
47
48
    public function open($path): self
49
    {
50
        $paths = Arr::wrap($path);
51
52
        foreach ($paths as $path) {
0 ignored issues
show
introduced by
$path is overwriting one of the parameters of this function.
Loading history...
53
            $this->collection->push(Media::make($this->disk, $path));
0 ignored issues
show
Bug introduced by
The method push() does not exist on Pbmedia\LaravelFFMpeg\Filesystem\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

53
            $this->collection->/** @scrutinizer ignore-call */ 
54
                               push(Media::make($this->disk, $path));
Loading history...
54
        }
55
56
        return $this;
57
    }
58
59
    public function get(): MediaCollection
60
    {
61
        return $this->collection;
62
    }
63
64
    public function getDriver(): PHPFFMpeg
65
    {
66
        return $this->driver->open($this->collection);
67
    }
68
69
    public function getAdvancedDriver(): PHPFFMpeg
70
    {
71
        return $this->driver->openAdvanced($this->collection);
72
    }
73
74
    public function getFrameFromString(string $timecode): self
75
    {
76
        return $this->getFrameFromTimecode(
77
            TimeCode::fromString($timecode)
78
        );
79
    }
80
81
    public function getFrameFromSeconds(float $quantity): self
82
    {
83
        return $this->getFrameFromTimecode(
84
            TimeCode::fromSeconds($quantity)
85
        );
86
    }
87
88
    public function getFrameFromTimecode(TimeCode $timecode): self
89
    {
90
        $this->timecode = $timecode;
91
92
        return $this;
93
    }
94
95
    public function export(): MediaExporter
96
    {
97
        return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) {
98
            if ($this->timecode) {
99
                $mediaExporter->frame($this->timecode);
0 ignored issues
show
Bug introduced by
The method frame() does not exist on Pbmedia\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

99
                $mediaExporter->/** @scrutinizer ignore-call */ 
100
                                frame($this->timecode);
Loading history...
100
            }
101
        });
102
    }
103
104
    public function exportForHLS(): HLSExporter
105
    {
106
        return new HLSExporter($this->getAdvancedDriver());
107
    }
108
109
    public function cleanupTemporaryFiles(): self
110
    {
111
        TemporaryDirectories::deleteAll();
112
113
        return $this;
114
    }
115
116
    public function __invoke(): AbstractMediaType
117
    {
118
        return $this->getDriver()->get();
119
    }
120
121
    public function __call($method, $arguments)
122
    {
123
        $result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments);
124
125
        return ($result === $driver) ? $this : $result;
126
    }
127
}
128