Completed
Pull Request — develop (#1350)
by Naveen
03:19
created

Video_Processor::get_vimeo_videos()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Videoobject;
4
5
use Wordlift\Videoobject\Data\Embedded_Video\Embedded_Video;
6
use Wordlift\Videoobject\Data\Video\Video;
7
use Wordlift\Videoobject\Data\Video_Storage\Storage;
8
use Wordlift\Videoobject\Data\Video_Storage\Video_Storage_Factory;
9
use Wordlift\Videoobject\Parser\Parser_Factory;
10
use Wordlift\Videoobject\Provider\Provider_Factory;
11
12
class Video_Processor {
13
14
	private function get_data_for_videos( $embedded_videos ) {
15
16
		$youtube_videos = $this->get_youtube_videos( $embedded_videos );
17
18
		$youtube_provider = Provider_Factory::get_provider( Provider_Factory::YOUTUBE );
19
		$youtube_videos   = $youtube_provider->get_videos_data( $youtube_videos );
20
21
		$vimeo_videos = $this->get_vimeo_videos( $embedded_videos );
22
23
		$vimeo_provider = Provider_Factory::get_provider( Provider_Factory::VIMEO );
24
		$vimeo_videos   = $vimeo_provider->get_videos_data( $vimeo_videos );
25
26
		return array_merge( $youtube_videos, $vimeo_videos );
27
28
	}
29
30
	/**
31
	 * @param $storage Storage
32
	 * @param $post_id int
33
	 * @param $embedded_videos array<Embedded_Video>
34
	 */
35
	private function remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ) {
36
37
		$videos_to_be_deleted = $this->get_videos_to_be_deleted( $storage, $post_id, $embedded_videos );
38
		$storage->remove_videos( $videos_to_be_deleted, $post_id );
39
40
	}
41
42
	/**
43
	 * @param Storage $storage
44
	 * @param $post_id
45
	 * @param array $embedded_videos
46
	 *
47
	 * @return array
48
	 */
49 View Code Duplication
	private function get_videos_to_be_deleted( Storage $storage, $post_id, array $embedded_videos ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
		$videos_in_store     = $storage->get_all_videos( $post_id );
51
		$embedded_video_urls = array_map( function ( $embedded_video ) {
52
			/**
53
			 * @var $embedded_video Embedded_Video
54
			 */
55
			return $embedded_video->get_url();
56
		}, $embedded_videos );
57
58
		return array_filter( $videos_in_store, function ( $video ) use ( $embedded_video_urls ) {
59
			/**
60
			 * @var $video Video
61
			 */
62
			return ! in_array( $video->id, $embedded_video_urls );
63
		} );
64
	}
65
66
	/**
67
	 * @param \WP_Post $post
68
	 * @param $post_id
69
	 */
70
	public function process_video_urls( \WP_Post $post, $post_id ) {
71
72
		$parser = Parser_Factory::get_parser_from_content( $post->post_content );
73
74
		$embedded_videos = $parser->get_videos( $post_id );
75
76
		$storage = Video_Storage_Factory::get_storage();
77
78
		// Before sending api requests we need to check if there are any videos in
79
		// store which is not present on post content, remove them if there are
80
		// any
81
		$this->remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos );
82
83
		$embedded_videos = $this->get_videos_without_existing_data( $storage, $post_id, $embedded_videos );
84
85
		// Return early after removing all the videos.
86
		if ( ! $embedded_videos ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $embedded_videos of type Wordlift\Videoobject\Dat..._Video\Embedded_Video[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
			return;
88
		}
89
90
		$videos = $this->get_data_for_videos( $embedded_videos );
91
92
		if ( ! $videos ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $videos of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
			return;
94
		}
95
96
		foreach ( $videos as $video ) {
97
			$storage->add_video( $post_id, $video );
98
		}
99
	}
100
101
	/**
102
	 * @param $storage Storage
103
	 * @param $post_id int
104
	 * @param $embedded_videos array<Embedded_Video>
105
	 *
106
	 * @return array<Embedded_Video> Return array of embedded videos which are not in store.
107
	 */
108 View Code Duplication
	private function get_videos_without_existing_data( $storage, $post_id, $embedded_videos ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
		$videos_in_store    = $storage->get_all_videos( $post_id );
110
		$video_ids_in_store = array_map( function ( $video ) {
111
			return $video->id;
112
		}, $videos_in_store );
113
114
		return array_filter( $embedded_videos, function ( $embedded_video ) use ( $video_ids_in_store ) {
115
			return ! in_array( $embedded_video->get_url(), $video_ids_in_store );
116
		} );
117
	}
118
119
	/**
120
	 * @param $embedded_videos
121
	 *
122
	 * @return array
123
	 */
124
	private function get_youtube_videos( $embedded_videos ) {
125
		return array_filter( $embedded_videos, function ( $embedded_video ) {
126
			/**
127
			 * it should have youtube.com or youtu.be in the url
128
			 *
129
			 * @param $embedded_video Embedded_Video
130
			 */
131
			$video_url = $embedded_video->get_url();
132
133
			return strpos( $video_url, "youtube.com" ) !== false ||
134
			       strpos( $video_url, "youtu.be" ) !== false;
135
		} );
136
	}
137
138
	/**
139
	 * @param $embedded_videos
140
	 *
141
	 * @return array
142
	 */
143
	private function get_vimeo_videos( $embedded_videos ) {
144
		return array_filter( $embedded_videos, function ( $embedded_video ) {
145
			/**
146
			 * it should have vimeo.com in the url
147
			 *
148
			 * @param $embedded_video Embedded_Video
149
			 */
150
			$video_url = $embedded_video->get_url();
151
152
			return strpos( $video_url, "vimeo.com" ) !== false;
153
		} );
154
	}
155
156
}