Completed
Push — master ( 3ee920...44cf32 )
by Pascal
02:41
created

MediaExporter::save()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 1
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use FFMpeg\Format\FormatInterface;
6
7
/**
8
 * @method mixed save(FormatInterface $format, $outputPathfile)
9
 */
10
class MediaExporter
11
{
12
    protected $media;
13
14
    protected $disk;
15
16
    protected $frameMustBeAccurate = false;
17
18
    protected $format;
19
20
    public function __construct(Media $media)
21
    {
22
        $this->media = $media;
23
        $this->disk  = $media->getFile()->getDisk();
24
    }
25
26
    protected function getFormat(): FormatInterface
27
    {
28
        return $this->format;
29
    }
30
31
    public function inFormat(FormatInterface $format): self
32
    {
33
        $this->format = $format;
34
35
        return $this;
36
    }
37
38
    protected function getDisk(): Disk
39
    {
40
        return $this->disk;
41
    }
42
43
    public function toDisk(string $diskName): self
44
    {
45
        $this->disk = Disk::fromName($diskName);
46
47
        return $this;
48
    }
49
50
    public function accurate(): self
51
    {
52
        $this->frameMustBeAccurate = true;
53
54
        return $this;
55
    }
56
57
    public function unaccurate(): self
58
    {
59
        $this->frameMustBeAccurate = false;
60
61
        return $this;
62
    }
63
64
    public function getAccuracy(): bool
65
    {
66
        return $this->frameMustBeAccurate;
67
    }
68
69
    public function save(string $path): Media
70
    {
71
        $disk = $this->getDisk();
72
        $file = $disk->newFile($path);
73
74
        $destination = $file->getFullPath();
75
76
        if (!$disk->isLocal()) {
77
            $destination = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg');
78
        }
79
80
        if ($this->media->isFrame()) {
81
            $this->saveFrame($destination);
82
        } else {
83
            $this->saveAudioOrVideo($destination);
84
        }
85
86
        if (!$disk->isLocal()) {
87
            $file->createFromTempPath($destination);
88
        }
89
90
        return $this->media;
91
    }
92
93
    private function saveFrame(string $fullPath): self
94
    {
95
        $this->media->save($fullPath, $this->getAccuracy());
0 ignored issues
show
Documentation Bug introduced by
The method save does not exist on object<Pbmedia\LaravelFFMpeg\Media>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
97
        return $this;
98
    }
99
100
    private function saveAudioOrVideo(string $fullPath): self
101
    {
102
        $this->media->save($this->getFormat(), $fullPath);
0 ignored issues
show
Documentation Bug introduced by
The method save does not exist on object<Pbmedia\LaravelFFMpeg\Media>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
104
        return $this;
105
    }
106
}
107