Passed
Push — main ( fcdcda...b78c1f )
by chief
02:32
created

video_helper::sh_exec()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 16
nop 5
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @Auther: bhief
5
* @Version: 1.0
6
*
7
* Gets info from videos 
8
*
9
**/
10
class video_helper {
11
    public $__db;
12
13
    public function sh_exec(string $cmd, string $outputfile = "", string $pidfile = "", bool $mergestderror = true, bool $bg = false) {
14
        $fullcmd = $cmd;
15
        if(strlen($outputfile) > 0) $fullcmd .= " >> " . $outputfile;
16
        if($mergestderror) $fullcmd .= " 2>&1";
17
        
18
        if($bg) {
19
            $fullcmd = "nohup " . $fullcmd . " &";
20
            if(strlen($pidfile)) $fullcmd .= " echo $! > " . $pidfile;
21
        } else {
22
            if(strlen($pidfile) > 0) $fullcmd .= "; echo $$ > " . $pidfile;
23
        }
24
        shell_exec($fullcmd);
25
    }
26
27
	public function __construct($conn){
28
        $this->__db = $conn;
29
	}
30
31
    function fetch_video_views(string $id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
        $stmt = $this->__db->prepare("SELECT * FROM views WHERE videoid = ?");
33
        $stmt->bind_param("s", $id);
34
        $stmt->execute();
35
        $result = $stmt->get_result();
36
        return $result->num_rows;
37
    }
38
39
    function get_comments_from_video($id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
        $stmt = $this->__db->prepare("SELECT * FROM comments WHERE toid = ?");
41
        $stmt->bind_param("s", $id);
42
        $stmt->execute();
43
        $result = $stmt->get_result();
44
        $rows = mysqli_num_rows($result); 
45
        $stmt->close();
46
    
47
        return $rows;
48
    }
49
50
    function shorten_description(string $description, int $limit, bool $newlines = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
        $description = trim($description);
52
        if(strlen($description) >= $limit) {
53
            $description = substr($description, 0, $limit) . "...";
54
        } 
55
56
        $description = htmlspecialchars($description);
57
        if($newlines) { $description = str_replace(PHP_EOL, "<br>", $description); }
58
        return $description;
59
    }
60
61
    function get_video_responses($id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
        $stmt = $this->__db->prepare("SELECT * FROM video_response WHERE toid = ?");
63
        $stmt->bind_param("s", $id);
64
        $stmt->execute();
65
        $result = $stmt->get_result();
66
        $rows = mysqli_num_rows($result); 
67
        $stmt->close();
68
    
69
        return $rows;
70
    }
71
72
    function fetch_video_rid(string $rid) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
            $stmt = $this->__db->prepare("SELECT * FROM videos WHERE rid = ?");
74
            $stmt->bind_param("s", $rid);
75
            $stmt->execute();
76
        $result = $stmt->get_result();
77
        $video = $result->fetch_assoc();
78
79
        if($result->num_rows === 0) 
80
            return 0;
81
        else
82
            return $video;
83
84
        $stmt->close();
0 ignored issues
show
Unused Code introduced by
$stmt->close() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
85
    }
86
87
    function get_video_stars_level($id, $level) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
        $stmt = $this->__db->prepare("SELECT * FROM stars WHERE reciever = ? AND type = ?");
89
        $stmt->bind_param("si", $id, $level);
90
        $stmt->execute();
91
        $result = $stmt->get_result();
92
        $rows = mysqli_num_rows($result); 
93
        $stmt->close();
94
    
95
        return $rows;
96
    }
97
98
    function video_exists($video) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
99
            $stmt = $this->__db->prepare("SELECT `rid` FROM videos WHERE rid = ?");
100
            $stmt->bind_param("s", $video);
101
            $stmt->execute();
102
        $result = $stmt->get_result();
103
        $user = $result->fetch_assoc();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
104
        if($result->num_rows === 1) { return true; } else { return false; }
105
        $stmt->close();
0 ignored issues
show
Unused Code introduced by
$stmt->close() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
106
        
107
        return $user;
108
    }
109
110
    function fetch_user_videos($v) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
111
        $stmt = $this->__db->prepare("SELECT rid FROM videos WHERE author = ?");
112
        $stmt->bind_param("s", $v);
113
        $stmt->execute(); 
114
        $result = $stmt->get_result();
115
        $rows = mysqli_num_rows($result); 
116
        $stmt->close();
117
    
118
        return $rows;
119
    }
120
}
121
122
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...