Completed
Pull Request — develop (#1350)
by Naveen
04:17 queued 01:06
created

Youtube   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_thumbnails() 0 6 1
A get_videos_data() 0 11 1
A get_data() 0 9 2
A parse_youtube_video_data_from_response() 0 17 4
C create_video_from_youtube_data() 0 62 11
1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Provider;
8
9
use DateInterval;
10
use Wordlift\Videoobject\Data\Video\Video;
11
12
class Youtube extends Api_Provider {
13
14
15
	const YT_API_FIELD_NAME = '__wl_video_object_youtube_api_key';
16
17
	private static function get_thumbnails( $api_thumbnail_data ) {
18
		return array_map( function ( $item ) {
19
			return $item['url'];
20
21
		}, $api_thumbnail_data );
22
	}
23
24
25
	public function get_videos_data( $videos ) {
26
		$urls = array_map( function ( $video ) {
27
			/**
28
			 * @param $video Video
29
			 */
30
			return $video->get_url();
31
		}, $videos );
32
33
		return $this->get_data( $urls );
34
35
	}
36
37
38
	/**
39
	 * @param $video_urls
40
	 *
41
	 * @return bool | array<Video>
42
	 */
43
	public function get_data( $video_urls ) {
44
		// extract ids from the url list.
45
		if ( ! is_array( $video_urls ) ) {
46
			return array();
47
		}
48
		$response_body = $this->api_client->get_data( $video_urls );
49
50
		return $this->parse_youtube_video_data_from_response( $response_body );
51
	}
52
53
54
	/**
55
	 * @param $response_body string
56
	 *
57
	 * @return array<Video>
58
	 */
59
	private function parse_youtube_video_data_from_response( $response_body ) {
60
		$result = json_decode( $response_body, true );
61
		if ( ! is_array( $result ) ) {
62
			// Return empty array since the response body is invalid.
63
			return array();
64
		}
65
		if ( ! array_key_exists( 'items', $result ) ) {
66
			return array();
67
		}
68
		$videos_json_data = $result['items'];
69
		$videos           = array();
70
		foreach ( $videos_json_data as $single_video_json_data ) {
71
			$videos[] = self::create_video_from_youtube_data( $single_video_json_data );
72
		}
73
74
		return $videos;
75
	}
76
77
	public static function create_video_from_youtube_data( $video_data ) {
78
79
		$video = new Video();
80
		if ( array_key_exists( 'contentDetails', $video_data ) ) {
81
			$video->duration = $video_data['contentDetails']['duration'];
82
		}
83
84
		if ( array_key_exists( 'id', $video_data ) ) {
85
			$video_id           = $video_data['id'];
86
			$video->embed_url   = "https://www.youtube.com/embed/${video_id}";
87
			$video->content_url = "https://www.youtube.com/watch?v=${video_id}";
88
		}
89
		if ( ! array_key_exists( 'snippet', $video_data ) ) {
90
			return false;
91
		}
92
93
		$video->name        = $video_data['snippet']['title'];
94
		$video->description = $video_data['snippet']['description'];
95
96
		/**
97
		 * @since 3.30.0
98
		 * Use title as fallback if description is not present.
99
		 */
100
		if ( ! $video->description ) {
101
			$video->description = $video->name;
102
		}
103
104
		$video->upload_date = $video_data['snippet']['publishedAt'];
105
106
		if ( array_key_exists( 'thumbnails', $video_data['snippet'] ) ) {
107
			$api_thumbnail_data    = array_values( $video_data['snippet']['thumbnails'] );
108
			$video->thumbnail_urls = self::get_thumbnails( $api_thumbnail_data );
109
		}
110
111
		if ( array_key_exists( 'statistics', $video_data )
112
		     && array_key_exists( 'viewCount', $video_data['statistics'] ) ) {
113
			$video->views = $video_data['statistics']['viewCount'];
114
		}
115
116
		if ( array_key_exists( 'liveStreamingDetails', $video_data ) &&
117
		     array_key_exists( 'scheduledStartTime', $video_data['liveStreamingDetails'] ) ) {
118
			$video->is_live_video         = true;
119
			$video->live_video_start_date = $video_data['liveStreamingDetails']['scheduledStartTime'];
120
			try {
121
				$end_date = new \DateTime( $video->live_video_start_date );
122
				/**
123
				 * the google doc says :
124
				 * It is required to provide the endDate once the video has finished and is no longer live.
125
				 * If the expected endDate is unknown prior to the livestream starting, we recommend providing an approximate endDate.
126
				 */
127
				// we add 1 day to start date
128
				$end_date->add( new DateInterval( 'P1D' ) );
129
				$video->live_video_end_date = $end_date->format( 'c' );
130
			} catch ( \Exception $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
131
			}
132
		}
133
134
		$video->id = $video->content_url;
135
136
		return $video;
137
138
	}
139
140
141
}