Passed
Push — master ( 2cd313...f3f6f1 )
by Amin
02:53
created

StreamingAnalytics::getStreamsMetadata()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 4
eloc 12
c 1
b 1
f 1
nc 4
nop 0
dl 0
loc 19
rs 9.8666
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
13
namespace Streaming;
14
15
16
class StreamingAnalytics
17
{
18
    /**
19
     * @var Export
20
     */
21
    private $export;
22
23
    /**
24
     * StreamingAnalytics constructor.
25
     * @param Export $export
26
     */
27
    public function __construct(Export $export)
28
    {
29
        $this->export = $export;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function __toString()
36
    {
37
        return json_encode($this->analyse());
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43
    public function analyse()
44
    {
45
        $metadata["original"] = $this->getOriginalMetadata();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$metadata was never initialized. Although not strictly required by PHP, it is generally a good practice to add $metadata = array(); before regardless.
Loading history...
46
        $metadata["streams"] = $this->getStreamsMetadata();
47
        $metadata["general"] = $this->getGeneralMetadata();
48
49
        $filename = $this->export->getPathInfo()["dirname"] . DIRECTORY_SEPARATOR . "analyse.json";
50
        file_put_contents($filename, json_encode($metadata));
51
52
        return $metadata;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    private function getOriginalMetadata()
59
    {
60
        $streams = $this->export
61
            ->getMedia()
62
            ->getAllStreams();
63
64
        foreach ($streams as $key => $stream){
65
            $streams[$key] = $stream->all();
66
        }
67
68
        return $streams;
69
    }
70
71
    /**
72
     * @return mixed
73
     */
74
    private function getStreamsMetadata()
75
    {
76
        $metadata["qualities"] = $this->getQualities();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$metadata was never initialized. Although not strictly required by PHP, it is generally a good practice to add $metadata = array(); before regardless.
Loading history...
77
78
        $format_class =  explode("\\" ,get_class($this->export->getFormat()));
79
        $metadata["format"] = end($format_class);
80
81
        $export_class =  explode("\\" ,get_class($this->export));
82
        $metadata["streaming_technique"] = end($export_class);
83
84
        if ($this->export instanceof DASH) {
85
            $metadata["dash_adaption"] = $this->export->getAdaption();
86
        } elseif ($this->export instanceof HLS) {
87
            $metadata["hls_time"] = $this->export->getHlsTime();
88
            $metadata["hls_cache"] = $this->export->isHlsAllowCache();
89
            $metadata["encrypted_hls"] = ($this->export->getHlsKeyInfoFile() !== "") ? true : false;
90
        }
91
92
        return $metadata;
93
    }
94
95
    private function getQualities()
96
    {
97
        $qualities = [];
98
        foreach ($this->export->getRepresentations() as $key => $representation) {
0 ignored issues
show
Bug introduced by
The method getRepresentations() does not exist on Streaming\Export. Since it exists in all sub-types, consider adding an abstract or default implementation to Streaming\Export. ( Ignorable by Annotation )

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

98
        foreach ($this->export->/** @scrutinizer ignore-call */ getRepresentations() as $key => $representation) {
Loading history...
99
            if ($representation instanceof Representation) {
100
                $qualities[$key]["dimensions"] = strtoupper($representation->getResize());
101
                $qualities[$key]["kilobitrate"] = $representation->getKiloBitrate();
102
            }
103
        }
104
105
        return $qualities;
106
    }
107
108
    private function getGeneralMetadata()
109
    {
110
        $video_path = $this->export->getMedia()->getPath();
111
        $metadata["path_to_video"] = $video_path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$metadata was never initialized. Although not strictly required by PHP, it is generally a good practice to add $metadata = array(); before regardless.
Loading history...
112
        $metadata["dir_path_to_video"] = pathinfo($video_path)["dirname"];
113
        $metadata["basename_of_video"] = pathinfo($video_path)["basename"];
114
        $metadata["extension_of_video"] = pathinfo($video_path)["extension"];
115
        $metadata["mime_content_type_of_video"] = !is_file($video_path)?:mime_content_type($video_path);
116
        $metadata["size_of_video"] = !is_file($video_path)?:filesize($video_path);
117
118
        $stream_path = $this->export->getPathInfo();
119
        $metadata["dir_path_to_stream"] = $stream_path["dirname"];
120
        $metadata["path_to_stream"] = $stream_path["dirname"] . DIRECTORY_SEPARATOR . $stream_path["basename"];
121
        $metadata["size_of_stream_dir"] = Helper::directorySize($stream_path["dirname"]);
122
        $metadata["datetime"] = date("Y-m-d H:i:s");
123
        $metadata["time"] = time();
124
125
        return $metadata;
126
    }
127
}