Completed
Push — master ( 90f2a9...a36dd1 )
by Pascal
01:24
created

MediaExporter::withVisibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use FFMpeg\Format\FormatInterface;
6
7
class MediaExporter
8
{
9
    protected $media;
10
11
    protected $disk;
12
13
    protected $format;
14
15
    protected $visibility;
16
17
    protected $saveMethod = 'saveAudioOrVideo';
18
19
    public function __construct(Media $media)
20
    {
21
        $this->media = $media;
22
23
        $this->disk = $media->getFile()->getDisk();
24
    }
25
26
    public function getFormat(): FormatInterface
27
    {
28
        return $this->format;
29
    }
30
31
    public function inFormat(FormatInterface $format): MediaExporter
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($diskOrName): MediaExporter
44
    {
45
        if ($diskOrName instanceof Disk) {
46
            $this->disk = $diskOrName;
47
        } else {
48
            $this->disk = Disk::fromName($diskOrName);
49
        }
50
51
        return $this;
52
    }
53
54
    public function withVisibility(string $visibility = null)
55
    {
56
        $this->visibility = $visibility;
57
58
        return $this;
59
    }
60
61
    public function save(string $path): Media
62
    {
63
        $disk = $this->getDisk();
64
        $file = $disk->newFile($path);
65
66
        $destinationPath = $this->getDestinationPathForSaving($file);
67
68
        $this->createDestinationPathForSaving($file);
69
70
        $this->{$this->saveMethod}($destinationPath);
71
72
        if (!$disk->isLocal()) {
73
            $this->moveSavedFileToRemoteDisk($destinationPath, $file);
74
        }
75
76
        if ($this->visibility) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->visibility of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
77
            $disk->setVisibility($path, $this->visibility);
0 ignored issues
show
Documentation Bug introduced by
The method setVisibility does not exist on object<Pbmedia\LaravelFFMpeg\Disk>? 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...
78
        }
79
80
        return $this->media;
81
    }
82
83
    protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool
84
    {
85
        return $fileOnRemoteDisk->put($localSourcePath) && @unlink($localSourcePath);
86
    }
87
88
    private function getDestinationPathForSaving(File $file): string
89
    {
90
        if (!$file->getDisk()->isLocal()) {
91
            $tempName = FFMpeg::newTemporaryFile();
92
93
            return $tempName . '.' . $file->getExtension();
94
        }
95
96
        return $file->getFullPath();
97
    }
98
99
    private function createDestinationPathForSaving(File $file)
100
    {
101
        if (!$file->getDisk()->isLocal()) {
102
            return false;
103
        }
104
105
        $directory = pathinfo($file->getPath())['dirname'];
106
107
        return $file->getDisk()->createDirectory($directory);
108
    }
109
110
    private function saveAudioOrVideo(string $fullPath): MediaExporter
111
    {
112
        $this->media->save($this->getFormat(), $fullPath);
113
114
        return $this;
115
    }
116
}
117