Passed
Push — master ( 1d1353...51bc60 )
by Pascal
02:58
created

MediaExporter::save()   C

Complexity

Conditions 13
Paths 116

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 116
nop 1
dl 0
loc 44
rs 6.4833
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 save(string $path = null)
96
    {
97
        $outputMedia = $path ? $this->getDisk()->makeMedia($path) : null;
98
99
        if ($this->concatWithTranscoding && $outputMedia) {
100
            $this->addConcatFilterAndMapping($outputMedia);
101
        }
102
103
        if ($this->maps->isNotEmpty()) {
104
            return $this->saveWithMappings();
105
        }
106
107
        if ($this->format && $this->onProgressCallback) {
108
            $this->applyProgressListenerToFormat($this->format);
109
        }
110
111
        if ($this->timelapseFramerate > 0) {
112
            $this->addTimelapseParametersToFormat();
113
        }
114
115
        if ($this->driver->isConcat() && $outputMedia) {
116
            $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

116
            $this->driver->/** @scrutinizer ignore-call */ 
117
                           saveFromSameCodecs($outputMedia->getLocalPath());
Loading history...
117
        } elseif ($this->driver->isFrame()) {
118
            $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

118
            /** @scrutinizer ignore-call */ 
119
            $data = $this->driver->save(
Loading history...
119
                optional($outputMedia)->getLocalPath(),
120
                $this->getAccuracy(),
121
                $this->returnFrameContents
122
            );
123
124
            if ($this->returnFrameContents) {
125
                return $data;
126
            }
127
        } else {
128
            $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

128
            $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...
129
        }
130
131
        $outputMedia->copyAllFromTemporaryDirectory($this->visibility);
132
        $outputMedia->setVisibility($this->visibility);
133
134
        if ($this->onProgressCallback) {
135
            call_user_func($this->onProgressCallback, 100, 0, 0);
136
        }
137
138
        return $this->getMediaOpener();
139
    }
140
141
    private function saveWithMappings(): MediaOpener
142
    {
143
        $this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps);
144
145
        $this->maps->map->apply($this->driver->get());
146
147
        if ($this->onProgressCallback) {
148
            $this->applyProgressListenerToFormat($this->maps->last()->getFormat());
149
        }
150
151
        $this->driver->save();
152
153
        if ($this->onProgressCallback) {
154
            call_user_func($this->onProgressCallback, 100);
155
        }
156
157
        $this->maps->map->getOutputMedia()->each->copyAllFromTemporaryDirectory($this->visibility);
158
159
        return $this->getMediaOpener();
160
    }
161
162
    protected function getMediaOpener(): MediaOpener
163
    {
164
        return new MediaOpener(
165
            $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

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