Passed
Push — master ( 5a13b9...a6a132 )
by Pascal
02:03
created

MediaExporter::dd()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Exporters;
4
5
use FFMpeg\Format\FormatInterface;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Traits\ForwardsCalls;
8
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
9
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk;
10
use ProtoneMedia\LaravelFFMpeg\MediaOpener;
11
12
/**
13
 * @mixin \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
14
 */
15
class MediaExporter
16
{
17
    use ForwardsCalls,
18
        HandlesAdvancedMedia,
19
        HandlesConcatenation,
20
        HandlesFrames,
21
        HandlesTimelapse,
22
        HasProgressListener;
23
24
    /**
25
     * @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg
26
     */
27
    protected $driver;
28
29
    /**
30
     * @var \FFMpeg\Format\FormatInterface
31
     */
32
    private $format;
33
34
    /**
35
     * @var string
36
     */
37
    protected $visibility;
38
39
    /**
40
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk
41
     */
42
    private $toDisk;
43
44
    public function __construct(PHPFFMpeg $driver)
45
    {
46
        $this->driver = $driver;
47
48
        $this->maps = new Collection;
49
    }
50
51
    protected function getDisk(): Disk
52
    {
53
        if ($this->toDisk) {
54
            return $this->toDisk;
55
        }
56
57
        $media = $this->driver->getMediaCollection();
58
59
        return $this->toDisk = $media->first()->getDisk();
0 ignored issues
show
Bug introduced by
The method first() 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

59
        return $this->toDisk = $media->/** @scrutinizer ignore-call */ first()->getDisk();
Loading history...
60
    }
61
62
    public function inFormat(FormatInterface $format): self
63
    {
64
        $this->format = $format;
65
66
        return $this;
67
    }
68
69
    public function toDisk($disk)
70
    {
71
        $this->toDisk = Disk::make($disk);
72
73
        return $this;
74
    }
75
76
    public function withVisibility(string $visibility)
77
    {
78
        $this->visibility = $visibility;
79
80
        return $this;
81
    }
82
83
    public function getCommand(string $path = null)
84
    {
85
        $this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps);
86
87
        $this->maps->each->apply($this->driver->get());
88
89
        return $this->driver->getFinalCommand(
0 ignored issues
show
Bug introduced by
The method getFinalCommand() does not exist on ProtoneMedia\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

89
        return $this->driver->/** @scrutinizer ignore-call */ getFinalCommand(
Loading history...
90
            $this->format,
91
            $path ? $this->getDisk()->makeMedia($path)->getLocalPath() : null
92
        );
93
    }
94
95
    public function dd(string $path = null)
96
    {
97
        dd($this->getCommand($path));
98
    }
99
100
    public function save(string $path = null)
101
    {
102
        $outputMedia = $path ? $this->getDisk()->makeMedia($path) : null;
103
104
        if ($this->concatWithTranscoding && $outputMedia) {
105
            $this->addConcatFilterAndMapping($outputMedia);
106
        }
107
108
        if ($this->maps->isNotEmpty()) {
109
            return $this->saveWithMappings();
110
        }
111
112
        if ($this->format && $this->onProgressCallback) {
113
            $this->applyProgressListenerToFormat($this->format);
114
        }
115
116
        if ($this->timelapseFramerate > 0) {
117
            $this->addTimelapseParametersToFormat();
118
        }
119
120
        if ($this->driver->isConcat() && $outputMedia) {
121
            $this->driver->saveFromSameCodecs($outputMedia->getLocalPath());
0 ignored issues
show
Bug introduced by
The method saveFromSameCodecs() does not exist on ProtoneMedia\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

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

123
            /** @scrutinizer ignore-call */ 
124
            $data = $this->driver->save(
Loading history...
124
                optional($outputMedia)->getLocalPath(),
125
                $this->getAccuracy(),
126
                $this->returnFrameContents
127
            );
128
129
            if ($this->returnFrameContents) {
130
                return $data;
131
            }
132
        } else {
133
            $this->driver->save($this->format, $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

133
            $this->driver->save($this->format, $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...
134
        }
135
136
        $outputMedia->copyAllFromTemporaryDirectory($this->visibility);
137
        $outputMedia->setVisibility($this->visibility);
138
139
        if ($this->onProgressCallback) {
140
            call_user_func($this->onProgressCallback, 100, 0, 0);
141
        }
142
143
        return $this->getMediaOpener();
144
    }
145
146
    private function saveWithMappings(): MediaOpener
147
    {
148
        $this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps);
149
150
        $this->maps->map->apply($this->driver->get());
151
152
        if ($this->onProgressCallback) {
153
            $this->applyProgressListenerToFormat($this->maps->last()->getFormat());
154
        }
155
156
        $this->driver->save();
157
158
        if ($this->onProgressCallback) {
159
            call_user_func($this->onProgressCallback, 100);
160
        }
161
162
        $this->maps->map->getOutputMedia()->each->copyAllFromTemporaryDirectory($this->visibility);
163
164
        return $this->getMediaOpener();
165
    }
166
167
    protected function getMediaOpener(): MediaOpener
168
    {
169
        return new MediaOpener(
170
            $this->driver->getMediaCollection()->last()->getDisk(),
0 ignored issues
show
Bug introduced by
The method last() 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

170
            $this->driver->getMediaCollection()->/** @scrutinizer ignore-call */ last()->getDisk(),
Loading history...
171
            $this->driver,
172
            $this->driver->getMediaCollection()
173
        );
174
    }
175
176
    /**
177
     * Forwards the call to the driver object and returns the result
178
     * if it's something different than the driver object itself.
179
     */
180
    public function __call($method, $arguments)
181
    {
182
        $result = $this->forwardCallTo($driver = $this->driver, $method, $arguments);
183
184
        return ($result === $driver) ? $this : $result;
185
    }
186
}
187