Passed
Push — master ( ac0e9d...b23da5 )
by Amin
04:21
created

Stream::pathInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        try {
122
            $this->media
123
                ->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

123
                ->/** @scrutinizer ignore-call */ 
124
                  addFilter($this->getFilter())
Loading history...
124
                ->save($this->getFormat(), $this->getPath());
0 ignored issues
show
Bug introduced by
The method save() 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

124
                ->/** @scrutinizer ignore-call */ save($this->getFormat(), $this->getPath());
Loading history...
125
        } catch (ExceptionInterface $e) {
126
            throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
127
        }
128
    }
129
130
    /**
131
     * @param $path
132
     * @param $clouds
133
     */
134
    private function paths(?string $path, array $clouds): void
135
    {
136
        if (!empty($clouds)) {
137
            $this->tmp_dir = File::tmpDir();
138
            $this->path = $this->tmp_dir . basename($path ?? $this->path);
139
        } elseif (!is_null($path)) {
140
            if (strlen($path) > PHP_MAXPATHLEN) {
141
                throw new InvalidArgumentException("The path is too long");
142
            }
143
144
            File::makeDir(dirname($path));
145
            $this->path = $path;
146
        } elseif ($this->media->isTmp()) {
147
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
148
        }
149
    }
150
151
    /**
152
     * @param string $path
153
     * @param array $clouds
154
     * @return mixed
155
     */
156
    public function save(string $path = null, array $clouds = []): Stream
157
    {
158
        $this->paths($path, $clouds);
159
        $this->run();
160
        $this->clouds($clouds, $path);
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param string $url
167
     */
168
    public function live(string $url): void
169
    {
170
        $this->path = $url;
171
        $this->run();
172
    }
173
174
    /**
175
     * @return Metadata
176
     */
177
    public function metadata(): Metadata
178
    {
179
        return new Metadata($this);
180
    }
181
182
    /**
183
     * clear tmp files
184
     */
185
    public function __destruct()
186
    {
187
        // make sure that FFmpeg process has benn terminated
188
        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

188
        sleep(/** @scrutinizer ignore-type */ .5);
Loading history...
189
        File::remove($this->tmp_dir);
190
191
        if ($this->media->isTmp()) {
192
            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

192
            File::remove(/** @scrutinizer ignore-type */ $this->media->getPathfile());
Loading history...
193
        }
194
    }
195
}