Passed
Push — master ( 04eafc...41f9d0 )
by Pascal
05:11 queued 02:24
created

MediaOnNetwork::toMedia()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filesystem;
4
5
use ProtoneMedia\LaravelFFMpeg\FFMpeg\InteractsWithHttpHeaders;
6
7
class MediaOnNetwork
8
{
9
    use InteractsWithHttpHeaders;
10
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    public function __construct(string $path, array $headers = [])
17
    {
18
        $this->path    = $path;
19
        $this->headers = $headers;
20
    }
21
22
    public static function make(string $path, array $headers = []): self
23
    {
24
        return new static($path, $headers);
25
    }
26
27
    public function getPath(): string
28
    {
29
        return $this->path;
30
    }
31
32
    public function getDisk(): Disk
33
    {
34
        return Disk::make(config('filesystems.default'));
35
    }
36
37
    public function getLocalPath(): string
38
    {
39
        return $this->path;
40
    }
41
42
    public function getFilenameWithoutExtension(): string
43
    {
44
        return pathinfo($this->getPath())['filename'];
45
    }
46
47
    public function getFilename(): string
48
    {
49
        return pathinfo($this->getPath())['basename'];
50
    }
51
52
    public function getCompiledHeaders(): array
53
    {
54
        return static::compileHeaders($this->getHeaders());
55
    }
56
57
    /**
58
     * Downloads the Media from the internet and stores it in
59
     * a temporary directory.
60
     *
61
     * @param callable $withCurl
62
     * @return \ProtoneMedia\LaravelFFMpeg\Filesystem\Media
63
     */
64
    public function toMedia(callable $withCurl = null): Media
65
    {
66
        $disk = Disk::makeTemporaryDisk();
67
68
        $curl = curl_init();
69
        curl_setopt($curl, CURLOPT_URL, $this->path);
0 ignored issues
show
Bug introduced by
It seems like $curl can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        curl_setopt(/** @scrutinizer ignore-type */ $curl, CURLOPT_URL, $this->path);
Loading history...
70
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
71
72
        if (!empty($this->headers)) {
73
            curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
74
        }
75
76
        if ($withCurl) {
77
            $curl = $withCurl($curl) ?: $curl;
78
        }
79
80
        $contents = curl_exec($curl);
81
        curl_close($curl);
82
83
        $disk->getFilesystemAdapter()->put(
84
            $filename = $this->getFilename(),
85
            $contents
86
        );
87
88
        return new Media($disk, $filename);
89
    }
90
}
91