GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/video-support ( 96814b )
by Brad
05:36
created

FooGallery_Pro_Video_Vimeo::fetch()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 3
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Created by PhpStorm.
5
 * User: steve
6
 * Date: 4/17/2018
7
 * Time: 10:09 PM
8
 */
9
10
if ( !class_exists("FooGallery_Pro_Video_Vimeo") ){
11
12
	require_once dirname(__FILE__) . '/class-foogallery-pro-video-base.php';
13
14
	class FooGallery_Pro_Video_Vimeo extends FooGallery_Pro_Video_Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
16
		// region Properties
17
18
		/**
19
		 * The regular expression used to match a Vimeo video URL.
20
		 * @var string
21
		 */
22
		public $regex_pattern;
23
24
		// endregion
25
26
		function __construct() {
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...
27
			$this->regex_pattern = '/(player\.)?vimeo\.com/i';
28
		}
29
30
		/**
31
		 * Takes a URL and checks if this class handles it.
32
		 *
33
		 * @param string $url The URL to check.
34
		 * @param array &$matches Optional. If matches is provided, it is passed to the `preg_match` call used to check the URL.
35
		 * @return int Returns 1 if the URL is handled, 0 if it is not, or FALSE if an error occurred.
36
		 */
37
		function handles($url, &$matches = array()){
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...
38
			return preg_match($this->regex_pattern, $url, $matches);
39
		}
40
41
		/**
42
		 * Takes the supplied Vimeo url and attempts to fetch its' data.
43
		 *
44
		 * @description At present this method supports the following url patterns:
45
		 *
46
		 * Single video urls
47
		 *
48
		 * - http(s)://vimeo.com/[VIDEO_ID]
49
		 * - http(s)://vimeo.com/album/[ALBUM_ID]/video/[VIDEO_ID]
50
		 * - http(s)://vimeo.com/channels/[CHANNEL_ID]/[VIDEO_ID]
51
		 *
52
		 * User urls
53
		 *
54
		 * - http(s)://vimeo.com/[USER_ID]
55
		 * - http(s)://vimeo.com/[USER_ID]/videos
56
		 *
57
		 * Album url
58
		 *
59
		 * - http(s)://vimeo.com/album/[ALBUM_ID]
60
		 *
61
		 * @param string $url The url to fetch the data for.
62
		 * @param int [$page=1] If this is a album or user url the page number could also be supplied.
63
		 * @param int [$offset=0] The number of items already retrieved for the query.
64
		 * @return array
65
		 */
66
		public function fetch($url, $page = 1, $offset = 0) {
67
			if ($this->handles($url)) {
68
				$matches = array();
69
70
				// check if we are dealing with an album
71
				if (preg_match('/vimeo\.com\/album\/(?<id>[0-9]*?)$/i', $url, $matches)) {
72
					// for albums the id is the last part of the url
73
					return $this->fetch_stream("album", $matches["id"], $page, $offset);
74
				}
75
76
				// check if we are dealing with a channel
77
				if (preg_match('/vimeo\.com\/channels\/(?<id>[0-9]*?)$/i', $url, $matches)) {
78
					// for albums the id is the last part of the url
79
					return $this->fetch_stream("channel", $matches["id"], $page, $offset);
80
				}
81
82
				// check if we are dealing with a video
83
				if (preg_match('/vimeo\.com\/(?:.*?\/)?(?<id>[0-9]*?)(?:\/)?$/i', $url, $matches)) {
84
					return $this->fetch_video($matches["id"]);
85
				}
86
87
				// check if we are dealing with a user url
88
				if (preg_match('/vimeo\.com\/(?<id>[a-zA-Z0-9]*?)(?:\/)?$/i', $url, $matches) || preg_match('/vimeo\.com\/(?<id>[a-zA-Z0-9]*?)\/videos(?:\/)?$/i', $url, $matches)) {
89
					return $this->fetch_stream("user", $matches["id"], $page, $offset);
90
				}
91
			}
92
			return $this->error_response("Unrecognized Vimeo url.");
93
		}
94
95
		public function fetch_video($id) {
96
			if (!is_numeric($id)) {
97
				return $this->error_response("Invalid video id supplied.");
98
			}
99
100
			// we have as valid an id as we can hope for until we make the actual request so request it
101
			$url = "https://vimeo.com/api/oembed.json?url=" . urlencode("https://vimeo.com/" . $id);
102
			// get the json object from the supplied url
103
			$json = $this->get_json($url);
104
105
			// if an error occurred return it
106
			if ($this->is_error($json)) {
107
				return $json;
108
			}
109
110
			// do basic validation on the parsed object
111
			if (empty($json) || empty($json->thumbnail_url) || empty($json->title)) {
112
				return $this->error_response("No video in response.");
113
			}
114
115
			$response = array(
116
				"mode" => "single",
117
				"videos" => array()
118
			);
119
120
			$response["videos"][] = array(
121
				"provider" => "vimeo",
122
				"id" => $id,
123
				"url" => "https://player.vimeo.com/video/" . $id,
124
				"thumbnail" => $json->thumbnail_url,
125
				"title" => $json->title,
126
				"description" => !empty($json->description) ? $json->description : ""
127
			);
128
129
			return $response;
130
		}
131
132
		public function fetch_stream($type, $id, $page = 1, $offset = 0) {
133
			$supports = array("album", "channel", "user");
134
135
			if (empty($type) || !is_string($type) || !in_array($type, $supports)) {
136
				return $this->error_response("Invalid type supplied.");
137
			}
138
139
			if (empty($id)) {
140
				return $this->error_response("Invalid id supplied.");
141
			}
142
143
			$endpoint = "https://player.vimeo.com/hubnut/config/%s/%s?page=%d";
144
145
			// we have as valid an id as we can hope for until we make the actual request so request it
146
			$url = sprintf($endpoint, $type, $id, $page);
147
			// get the json object from the supplied url
148
			$json = $this->get_json($url);
149
150
			// if an error occurred return it
151
			if ($this->is_error($json)) {
152
				return $json;
153
			}
154
155
			// do basic validation on the json object
156
			if (empty($json) || !is_object($stream = $json->stream) || !is_array($stream->clips)) {
157
				return $this->error_response("Invalid response.");
158
			}
159
160
			// if we get here we need to build a response to return to the frontend
161
			$response = array(
162
				"mode" => $type,
163
				"id" => $id,
164
				"total" => $stream->total_clips,
165
				"offset" => $offset,
166
				"page" => $page,
167
				"nextPage" => 0,
168
				"videos" => array()
169
			);
170
171
			$has_valid = false;
172
			// iterate each of the returned videos and add them to the response in our desired format
173
			foreach ($stream->clips as $video) {
174
				if (!empty($video->thumbnail) && !empty($video->id) && !empty($video->title)) {
175
					if (!$has_valid) $has_valid = true;
176
					$response["videos"][] = array(
177
						"provider" => "vimeo",
178
						"id" => $video->id,
179
						"url" => "https://player.vimeo.com/video/" . $video->id,
180
						"thumbnail" => $video->thumbnail,
181
						"title" => $video->title,
182
						"description" => !empty($video->description) ? $video->description : ""
183
					);
184
				}
185
			}
186
187
			if (!$has_valid) {
188
				return $this->error_response("No valid videos in response.");
189
			}
190
191
			$response["offset"] = $response["offset"] + count($response["videos"]);
192
			if ($response["total"] > $response["offset"]){
193
				$response["nextPage"] = $response["page"] + 1;
194
			}
195
196
			return $response;
197
		}
198
199
200
	}
201
202
}