Stream   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 175
rs 10
c 0
b 0
f 0
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A isTmpDir() 0 3 1
A getMedia() 0 3 1
A pathInfo() 0 3 1
A moveTmp() 0 6 3
A __construct() 0 4 1
A getFilePath() 0 6 1
A clouds() 0 5 2
A __destruct() 0 8 2
A metadata() 0 3 1
A run() 0 12 2
A live() 0 4 1
A paths() 0 14 5
A save() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Streaming;
13
14
use FFMpeg\Exception\ExceptionInterface;
15
use Streaming\Clouds\Cloud;
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\Exception\RuntimeException;
18
use Streaming\Filters\StreamFilterInterface;
19
use Streaming\Traits\Formats;
20
21
22
abstract class Stream implements StreamInterface
23
{
24
    use Formats;
25
26
    /** @var Media */
27
    private $media;
28
29
    /** @var string */
30
    protected $path;
31
32
    /** @var string */
33
    private $tmp_dir = '';
34
35
    /**
36
     * Stream constructor.
37
     * @param Media $media
38
     */
39
    public function __construct(Media $media)
40
    {
41
        $this->media = $media;
42
        $this->path = $media->getPathfile();
0 ignored issues
show
Documentation Bug introduced by
It seems like $media->getPathfile() of type FFMpeg\Media\Video or Streaming\Media is incompatible with the declared type string of property $path.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Bug introduced by
The method getPathfile() does not exist on Streaming\Media. 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

42
        /** @scrutinizer ignore-call */ 
43
        $this->path = $media->getPathfile();
Loading history...
43
    }
44
45
    /**
46
     * @return Media
47
     */
48
    public function getMedia(): Media
49
    {
50
        return $this->media;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isTmpDir(): bool
57
    {
58
        return (bool)$this->tmp_dir;
59
    }
60
61
    /**
62
     * @param int $option
63
     * @return string
64
     */
65
    public function pathInfo(int $option): string
66
    {
67
        return pathinfo($this->path, $option);
68
    }
69
70
    /**
71
     * @param string|null $path
72
     */
73
    private function moveTmp(?string $path): void
74
    {
75
        if ($this->isTmpDir() && !is_null($path)) {
76
            File::move($this->tmp_dir, dirname($path));
77
            $this->path = $path;
78
            $this->tmp_dir = '';
79
        }
80
    }
81
82
    /**
83
     * @param array $clouds
84
     * @param string $path
85
     */
86
    private function clouds(array $clouds, ?string $path): void
87
    {
88
        if (!empty($clouds)) {
89
            Cloud::uploadDirectory($clouds, $this->tmp_dir);
90
            $this->moveTmp($path);
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function getFilePath(): string
98
    {
99
        return str_replace(
100
            "\\",
101
            "/",
102
            $this->pathInfo(PATHINFO_DIRNAME) . "/" . $this->pathInfo(PATHINFO_FILENAME)
103
        );
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    abstract protected function getPath(): string;
110
111
    /**
112
     * @return StreamFilterInterface
113
     */
114
    abstract protected function getFilter(): StreamFilterInterface;
115
116
    /**
117
     * Run FFmpeg to package media content
118
     */
119
    private function run(): void
120
    {
121
        $this->media->addFilter($this->getFilter());
0 ignored issues
show
Bug introduced by
The method addFilter() does not exist on Streaming\Media. 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

121
        $this->media->/** @scrutinizer ignore-call */ 
122
                      addFilter($this->getFilter());
Loading history...
122
123
        $commands = (new CommandBuilder($this->media, $this->getFormat()))->build($this->getFormat(), $this->getPath());
124
        $pass = $this->format->getPasses();
125
        $listeners = $this->format->createProgressListener($this->media->baseMedia(), $this->media->getFFProbe(), 1, $pass);
0 ignored issues
show
Bug introduced by
$this->media->getFFProbe() of type FFMpeg\Media\Video|Streaming\Media is incompatible with the type FFMpeg\FFProbe expected by parameter $ffprobe of FFMpeg\Format\Video\Defa...reateProgressListener(). ( Ignorable by Annotation )

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

125
        $listeners = $this->format->createProgressListener($this->media->baseMedia(), /** @scrutinizer ignore-type */ $this->media->getFFProbe(), 1, $pass);
Loading history...
Bug introduced by
The method getFFProbe() does not exist on Streaming\Media. 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

125
        $listeners = $this->format->createProgressListener($this->media->baseMedia(), $this->media->/** @scrutinizer ignore-call */ getFFProbe(), 1, $pass);
Loading history...
126
127
        try {
128
            $this->media->getFFMpegDriver()->command($commands, false, $listeners);
0 ignored issues
show
Bug introduced by
The method command() does not exist on Streaming\Media. 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

128
            $this->media->getFFMpegDriver()->/** @scrutinizer ignore-call */ command($commands, false, $listeners);
Loading history...
Bug introduced by
The method getFFMpegDriver() does not exist on Streaming\Media. 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

128
            $this->media->/** @scrutinizer ignore-call */ 
129
                          getFFMpegDriver()->command($commands, false, $listeners);
Loading history...
Bug introduced by
The method command() does not exist on FFMpeg\Media\Video. ( 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->media->getFFMpegDriver()->/** @scrutinizer ignore-call */ command($commands, false, $listeners);

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
        } catch (ExceptionInterface $e) {
130
            throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
131
        }
132
    }
133
134
    /**
135
     * @param $path
136
     * @param $clouds
137
     */
138
    private function paths(?string $path, array $clouds): void
139
    {
140
        if (!empty($clouds)) {
141
            $this->tmp_dir = File::tmpDir();
142
            $this->path = $this->tmp_dir . basename($clouds['options']['filename'] ?? $path ?? $this->path);
143
        } elseif (!is_null($path)) {
144
            if (strlen($path) > PHP_MAXPATHLEN) {
145
                throw new InvalidArgumentException("The path is too long");
146
            }
147
148
            File::makeDir(dirname($path));
149
            $this->path = $path;
150
        } elseif ($this->media->isTmp()) {
151
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
152
        }
153
    }
154
155
    /**
156
     * @param string $path
157
     * @param array $clouds
158
     * @return mixed
159
     */
160
    public function save(string $path = null, array $clouds = []): Stream
161
    {
162
        $this->paths($path, $clouds);
163
        $this->run();
164
        $this->clouds($clouds, $path);
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param string $url
171
     */
172
    public function live(string $url): void
173
    {
174
        $this->path = $url;
175
        $this->run();
176
    }
177
178
    /**
179
     * @return Metadata
180
     */
181
    public function metadata(): Metadata
182
    {
183
        return new Metadata($this);
184
    }
185
186
    /**
187
     * clear tmp files
188
     */
189
    public function __destruct()
190
    {
191
        // make sure that FFmpeg process has benn terminated
192
        sleep(.5);
0 ignored issues
show
Bug introduced by
0.5 of type double is incompatible with the type integer expected by parameter $seconds of sleep(). ( Ignorable by Annotation )

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

192
        sleep(/** @scrutinizer ignore-type */ .5);
Loading history...
193
        File::remove($this->tmp_dir);
194
195
        if ($this->media->isTmp()) {
196
            File::remove($this->media->getPathfile());
0 ignored issues
show
Bug introduced by
$this->media->getPathfile() of type FFMpeg\Media\Video|Streaming\Media is incompatible with the type string expected by parameter $dir of Streaming\File::remove(). ( Ignorable by Annotation )

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

196
            File::remove(/** @scrutinizer ignore-type */ $this->media->getPathfile());
Loading history...
197
        }
198
    }
199
}