Completed
Push — master ( 847256...9b93e4 )
by Pascal
06:33
created

FFMpeg::newTemporaryFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg;
4
5
use FFMpeg\FFMpeg as BaseFFMpeg;
6
use Illuminate\Contracts\Config\Repository as ConfigRepository;
7
use Illuminate\Contracts\Filesystem\Factory as Filesystems;
8
use Psr\Log\LoggerInterface;
9
10
class FFMpeg
11
{
12
    protected static $filesystems;
13
14
    private static $temporaryFiles = [];
15
16
    protected $disk;
17
18
    protected $ffmpeg;
19
20
    public function __construct(Filesystems $filesystems, ConfigRepository $config, LoggerInterface $logger)
21
    {
22
        static::$filesystems = $filesystems;
23
24
        $ffmpegConfig = $config->get('laravel-ffmpeg');
25
26
        $this->ffmpeg = BaseFFMpeg::create($ffmpegConfig, $logger);
27
        $this->fromDisk($ffmpegConfig['default_disk'] ?? $config->get('filesystems.default'));
28
    }
29
30
    public static function getFilesystems(): Filesystems
31
    {
32
        return static::$filesystems;
33
    }
34
35
    public static function newTemporaryFile(): string
36
    {
37
        return static::$temporaryFiles[] = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg');
0 ignored issues
show
Bug introduced by
Since $temporaryFiles is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $temporaryFiles to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
38
    }
39
40
    public function cleanupTemporaryFiles()
41
    {
42
        foreach (static::$temporaryFiles as $path) {
0 ignored issues
show
Bug introduced by
Since $temporaryFiles is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $temporaryFiles to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
43
            @unlink($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
        }
45
    }
46
47
    public function fromDisk(string $diskName): FFMpeg
48
    {
49
        $filesystem = static::getFilesystems()->disk($diskName);
50
        $this->disk = new Disk($filesystem);
51
52
        return $this;
53
    }
54
55
    public function open($path): Media
56
    {
57
        $file = $this->disk->newFile($path);
58
59
        if ($this->disk->isLocal()) {
60
            $ffmpegPathFile = $file->getFullPath();
61
        } else {
62
            $ffmpegPathFile = static::newTemporaryFile();
63
            file_put_contents($ffmpegPathFile, $this->disk->read($path));
0 ignored issues
show
Documentation Bug introduced by
The method read 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...
64
        }
65
66
        $ffmpegMedia = $this->ffmpeg->open($ffmpegPathFile);
67
68
        return new Media($file, $ffmpegMedia);
69
    }
70
}
71