1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LearnParty\Http\Repositories; |
4
|
|
|
|
5
|
|
|
use LearnParty\Video; |
6
|
|
|
use LearnParty\Comment; |
7
|
|
|
use LearnParty\Favorite; |
8
|
|
|
use LearnParty\User; |
9
|
|
|
use Auth; |
10
|
|
|
|
11
|
|
|
class VideoRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get a single video |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function getVideo($id) |
19
|
|
|
{ |
20
|
|
|
return Video::findOrFail($id); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Update the number of views by one everytime a video is viewed |
25
|
|
|
* |
26
|
|
|
* @param Integer $id video Id |
27
|
|
|
* @return voi |
28
|
|
|
*/ |
29
|
|
|
public function updateViews($id) |
30
|
|
|
{ |
31
|
|
|
Video::find($id)->increment('views'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return all comments as well as the respective users |
36
|
|
|
* |
37
|
|
|
* @param integer $videoId Video Id |
38
|
|
|
* @return Object Comments collection |
39
|
|
|
*/ |
40
|
|
|
public function getAllComments($videoId) |
41
|
|
|
{ |
42
|
|
|
return Comment::where('video_id', $videoId)->get(); |
43
|
|
|
$comments = $comments->each(function ($comment, $key) { |
|
|
|
|
44
|
|
|
$comment['user'] = User::find($comment->user_id); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Parse the Youtube Url. |
50
|
|
|
* |
51
|
|
|
* @param string $url Youtube Url |
52
|
|
|
* @return string Embed link |
53
|
|
|
*/ |
54
|
|
|
public function makeYoutubeUrl($url) |
55
|
|
|
{ |
56
|
|
|
return substr(parse_url($url)['query'], 2, 11); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return true if a user has liked a video and |
61
|
|
|
* false if the user has not liked the vide. |
62
|
|
|
* |
63
|
|
|
* @param Collection $video Video collection |
64
|
|
|
* @return boolean True or false |
65
|
|
|
*/ |
66
|
|
|
public function getLikeStatus($video) |
67
|
|
|
{ |
68
|
|
|
if (Auth::user()) { |
69
|
|
|
$favorites = $video->favorites; |
70
|
|
|
foreach ($favorites as $key => $favorite) { |
71
|
|
|
if ($favorite->user_id === Auth::user()->id) { |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Favorite a video. |
81
|
|
|
* |
82
|
|
|
* @param Request $request User request |
83
|
|
|
* @return array New favorite message |
84
|
|
|
*/ |
85
|
|
|
public function favoriteVideo($request) |
86
|
|
|
{ |
87
|
|
|
Favorite::create($request); |
88
|
|
|
return [ |
89
|
|
|
'status' => 200, |
90
|
|
|
'message' => 'favorited' |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Unfavorite a video. |
96
|
|
|
* |
97
|
|
|
* @param Request $request User request |
98
|
|
|
* @return array New unfavorite message |
99
|
|
|
*/ |
100
|
|
|
public function unfavoriteVideo($request) |
101
|
|
|
{ |
102
|
|
|
$favorited = Favorite::where('user_id', Auth::user()->id) |
103
|
|
|
->where('video_id', $request['video_id']) |
104
|
|
|
->delete(); |
105
|
|
|
|
106
|
|
|
if ($favorited) { |
107
|
|
|
return [ |
108
|
|
|
'status' => 200, |
109
|
|
|
'message' => 'unfavorited' |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return [ |
114
|
|
|
'status' => 400, |
115
|
|
|
'message' => 'error. Something went wrong. Please try again' |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* If the person trying to edit or delete a video is not the |
121
|
|
|
* owner, redirect to the homepage |
122
|
|
|
* |
123
|
|
|
* @param Object $video Video collection |
124
|
|
|
* @return boolean true if valid, otherwise false |
125
|
|
|
*/ |
126
|
|
|
public function authorized($video) |
127
|
|
|
{ |
128
|
|
|
if (Auth::user()->id == $video->user_id) { |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the most popular videos in the followin categories |
137
|
|
|
* Liked |
138
|
|
|
* commented on |
139
|
|
|
* Favorited |
140
|
|
|
* |
141
|
|
|
* @return array top popular videos |
142
|
|
|
*/ |
143
|
|
|
public function getTopPopularVideos($number = 3) |
144
|
|
|
{ |
145
|
|
|
$topViewed = Video::orderBy('views', 'DESC')->get()->take($number); |
146
|
|
|
|
147
|
|
|
$topFavorited = Video::all()->each(function ($video, $key) { |
|
|
|
|
148
|
|
|
$video->favorites = $video->favorites->count(); |
149
|
|
|
})->sortByDesc('favorites')->take($number); |
150
|
|
|
|
151
|
|
|
$topCommentedOn = Video::all()->each(function ($video, $key) { |
|
|
|
|
152
|
|
|
$video->comments = $video->comments->count(); |
153
|
|
|
})->sortByDesc('comments')->take($number); |
154
|
|
|
|
155
|
|
|
$topUsers = User::all()->each(function ($user, $key) { |
|
|
|
|
156
|
|
|
$user->videos = $user->videos->count(); |
157
|
|
|
})->sortByDesc('videos')->take(5); |
158
|
|
|
|
159
|
|
|
return [ |
160
|
|
|
'topViewed' => $topViewed, |
161
|
|
|
'topFavorited' => $topFavorited, |
162
|
|
|
'topCommentedOn' => $topCommentedOn, |
163
|
|
|
'topUsers' => $topUsers, |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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
orexit
statements that have been added for debug purposes.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.