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

MediaExporter::getCommand()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg\Exporters;
4
5
use FFMpeg\Format\FormatInterface;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Traits\ForwardsCalls;
8
use Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
9
use Pbmedia\LaravelFFMpeg\Filesystem\Disk;
10
use Pbmedia\LaravelFFMpeg\MediaOpener;
11
12
class MediaExporter
13
{
14
    use ForwardsCalls,
15
        HandlesAdvancedMedia,
16
        HandlesConcatenation,
17
        HandlesFrames,
18
        HandlesTimelapse,
19
        HasProgressListener;
20
21
    protected PHPFFMpeg $driver;
22
    private ?FormatInterface $format = null;
23
    protected ?string $visibility    = null;
24
    private ?Disk $toDisk            = null;
25
26
    public function __construct(PHPFFMpeg $driver)
27
    {
28
        $this->driver = $driver;
29
30
        $this->maps = new Collection;
31
    }
32
33
    protected function getDisk(): Disk
34
    {
35
        if ($this->toDisk) {
36
            return $this->toDisk;
37
        }
38
39
        $media = $this->driver->getMediaCollection();
40
41
        return $this->toDisk = $media->first()->getDisk();
0 ignored issues
show
Bug introduced by
The method first() 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

41
        return $this->toDisk = $media->/** @scrutinizer ignore-call */ first()->getDisk();
Loading history...
42
    }
43
44
    public function inFormat(FormatInterface $format): self
45
    {
46
        $this->format = $format;
47
48
        return $this;
49
    }
50
51
    public function toDisk($disk)
52
    {
53
        $this->toDisk = Disk::make($disk);
54
55
        return $this;
56
    }
57
58
    public function withVisibility(string $visibility)
59
    {
60
        $this->visibility = $visibility;
61
62
        return $this;
63
    }
64
65
    public function getCommand(string $path = null)
66
    {
67
        $this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps);
68
69
        $this->maps->each->apply($this->driver->get());
70
71
        return $this->driver->getFinalCommand(
0 ignored issues
show
Bug introduced by
The method getFinalCommand() does not exist on Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg. 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

71
        return $this->driver->/** @scrutinizer ignore-call */ getFinalCommand(
Loading history...
72
            $this->format,
73
            $path ? $this->getDisk()->makeMedia($path)->getLocalPath() : null
74
        );
75
    }
76
77
    public function save(string $path = null)
78
    {
79
        $outputMedia = $path ? $this->getDisk()->makeMedia($path) : null;
80
81
        if ($this->concatWithTranscoding) {
82
            $this->addConcatFilterAndMapping($outputMedia);
0 ignored issues
show
Bug introduced by
It seems like $outputMedia can also be of type null; however, parameter $outputMedia of Pbmedia\LaravelFFMpeg\Ex...oncatFilterAndMapping() does only seem to accept Pbmedia\LaravelFFMpeg\Filesystem\Media, maybe add an additional type check? ( Ignorable by Annotation )

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

82
            $this->addConcatFilterAndMapping(/** @scrutinizer ignore-type */ $outputMedia);
Loading history...
83
        }
84
85
        if ($this->maps->isNotEmpty()) {
86
            return $this->saveWithMappings();
87
        }
88
89
        if ($this->format && $this->onProgressCallback) {
90
            $this->applyProgressListenerToFormat($this->format);
91
        }
92
93
        if ($this->timelapseFramerate > 0) {
94
            $this->addTimelapseParametersToFormat();
95
        }
96
97
        if ($this->driver->isConcat()) {
98
            $this->driver->saveFromSameCodecs($outputMedia->getLocalPath());
0 ignored issues
show
Bug introduced by
The method getLocalPath() does not exist on null. ( Ignorable by Annotation )

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

98
            $this->driver->saveFromSameCodecs($outputMedia->/** @scrutinizer ignore-call */ getLocalPath());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method saveFromSameCodecs() does not exist on Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg. 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

98
            $this->driver->/** @scrutinizer ignore-call */ 
99
                           saveFromSameCodecs($outputMedia->getLocalPath());
Loading history...
99
        } elseif ($this->driver->isFrame()) {
100
            $data = $this->driver->save(
0 ignored issues
show
Bug introduced by
The method save() does not exist on Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg. 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

100
            /** @scrutinizer ignore-call */ 
101
            $data = $this->driver->save(
Loading history...
101
                optional($outputMedia)->getLocalPath(),
102
                $this->getAccuracy(),
103
                $this->returnFrameContents
104
            );
105
106
            if ($this->returnFrameContents) {
107
                return $data;
108
            }
109
        } else {
110
            $this->driver->save($this->format, $outputMedia->getLocalPath());
111
        }
112
113
        $outputMedia->copyAllFromTemporaryDirectory($this->visibility);
114
        $outputMedia->setVisibility($this->visibility);
115
116
        if ($this->onProgressCallback) {
117
            call_user_func($this->onProgressCallback, 100);
118
        }
119
120
        return $this->getMediaOpener();
121
    }
122
123
    private function saveWithMappings(): MediaOpener
124
    {
125
        $this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps);
126
127
        $this->maps->map->apply($this->driver->get());
128
129
        if ($this->onProgressCallback) {
130
            $this->applyProgressListenerToFormat($this->maps->last()->getFormat());
131
        }
132
133
        $this->driver->save();
134
135
        if ($this->onProgressCallback) {
136
            call_user_func($this->onProgressCallback, 100);
137
        }
138
139
        $this->maps->map->getOutputMedia()->each->copyAllFromTemporaryDirectory($this->visibility);
140
141
        return $this->getMediaOpener();
142
    }
143
144
    protected function getMediaOpener(): MediaOpener
145
    {
146
        return new MediaOpener(
147
            $this->driver->getMediaCollection()->last()->getDisk(),
0 ignored issues
show
Bug introduced by
The method last() 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

147
            $this->driver->getMediaCollection()->/** @scrutinizer ignore-call */ last()->getDisk(),
Loading history...
148
            $this->driver->fresh(),
149
            $this->driver->getMediaCollection()
150
        );
151
    }
152
153
    /**
154
     * Forwards the call to the driver object and returns the result
155
     * if it's something different than the driver object itself.
156
     */
157
    public function __call($method, $arguments)
158
    {
159
        $result = $this->forwardCallTo($driver = $this->driver, $method, $arguments);
160
161
        return ($result === $driver) ? $this : $result;
162
    }
163
}
164