Completed
Push — develop ( 6b6dc0...30e23b )
by
unknown
06:58 queued 03:34
created

Jsonld::wl_after_get_jsonld()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 12
nop 3
dl 0
loc 38
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @since 3.31.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Videoobject\Jsonld;
8
9
10
use Wordlift\Videoobject\Data\Video\Video;
11
use Wordlift\Videoobject\Data\Video_Storage\Storage;
12
13
class Jsonld {
14
	/**
15
	 * @var Storage
16
	 */
17
	private $video_storage;
18
19
	/**
20
	 * Jsonld constructor.
21
	 *
22
	 * @param $video_storage Storage
23
	 */
24
	public function __construct( $video_storage ) {
25
		add_action( 'wl_post_jsonld', array( $this, 'wl_post_jsonld' ), 10, 3 );
26
		add_action( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 3 );
27
		$this->video_storage = $video_storage;
28
	}
29
30
31
	public function wl_after_get_jsonld( $jsonld, $post_id, $context ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
		if ( 0 === count( $jsonld ) ) {
33
			return $jsonld;
34
		}
35
		$current_item = $jsonld[0];
36
37
		if ( ! array_key_exists( '@type', $current_item ) ) {
38
			// Cant determine type return early.
39
			return $jsonld;
40
		}
41
42
		$type = $current_item['@type'];
43
		if ( ( is_string( $type ) && $type === 'Article' ) ||
44
		     ( is_array( $type ) && in_array( 'Article', $type ) ) ) {
45
			return $jsonld;
46
		}
47
48
		$videos_jsonld = $this->get_videos_jsonld( $post_id );
49
		if ( 0 === count( $videos_jsonld ) ) {
50
			return $jsonld;
51
		}
52
53
		// check if we have @id in jsonld for first item.
54
		$id = array_key_exists( '@id', $current_item ) ? $current_item['@id'] : '';
55
56
		foreach ( $videos_jsonld as &$video_jsonld ) {
57
			if ( ! $id) {
58
				continue;
59
			}
60
			if ( ! array_key_exists( 'mentions', $video_jsonld ) ) {
61
				$video_jsonld['mentions'] = array( '@id' => $id );
62
			} else {
63
				$video_jsonld['mentions'] = array_merge( $video_jsonld['mentions'], array( '@id' => $id ) );
64
			}
65
		}
66
67
		return array_merge( $jsonld, $videos_jsonld );
68
	}
69
70
	/**
71
	 * @param $existing_video_data string | array associative or sequential array.
72
	 * @param $new_video_data array Sequential array.
73
	 *
74
	 * @return array
75
	 */
76
	private function merge_video_data( $existing_video_data, $new_video_data ) {
77
		if ( ! is_array( $existing_video_data ) ) {
78
			$new_video_data[] = $existing_video_data;
79
80
			return $new_video_data;
81
		}
82
83
		if ( $this->is_associative_array( $existing_video_data ) ) {
84
			$new_video_data[] = $existing_video_data;
85
86
			return $new_video_data;
87
		}
88
89
		return array_merge( $existing_video_data, $new_video_data );
90
	}
91
92
	public function wl_post_jsonld( $jsonld, $post_id, $references ) {
0 ignored issues
show
Unused Code introduced by
The parameter $references is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
94
		$video_jsonld = $this->get_videos_jsonld( $post_id );
95
		if ( count( $video_jsonld ) === 0 ) {
96
			return $jsonld;
97
		}
98
		// Before adding the video jsonld check if the key
99
		// is present and additional data might be present,
100
		// if not present just add the data and return early.
101
		if ( ! array_key_exists( 'video', $jsonld ) ) {
102
			$jsonld['video'] = $video_jsonld;
103
104
			return $jsonld;
105
		}
106
107
		// since key exists, we need to merge the data based on type.
108
		$previous_video_data = $jsonld['video'];
109
		$jsonld['video']     = $this->merge_video_data( $previous_video_data, $video_jsonld );
110
111
		return $jsonld;
112
	}
113
114
115
	/**
116
	 * @param $post_id int Post id.
117
	 *
118
	 * @return array
119
	 */
120
	public function get_videos_jsonld( $post_id ) {
121
122
		$videos = $this->video_storage->get_all_videos( $post_id );
123
124
		$jsonld = array();
125
126
		foreach ( $videos as $video ) {
127
			/**
128
			 * @var $video Video
129
			 */
130
			$single_jsonld = array(
131
				'@context'     => 'http://schema.org',
132
				'@type'        => 'VideoObject',
133
				'name'         => $video->name,
134
				'description'  => $video->description,
135
				'contentUrl'   => $video->content_url,
136
				'embedUrl'     => $video->embed_url,
137
				'uploadDate'   => $video->upload_date,
138
				'thumbnailUrl' => $video->thumbnail_urls,
139
				'duration'     => $video->duration,
140
			);
141
142
			if ( $video->views ) {
143
				$single_jsonld['interactionStatistic'] = array(
144
					'@type'                => 'InteractionCounter',
145
					'interactionType'      => array(
146
						'@type' => 'http://schema.org/WatchAction'
147
					),
148
					'userInteractionCount' => $video->views
149
				);
150
			}
151
152
			if ( $video->is_live_video ) {
153
				$single_jsonld['publication'] = array(
154
					'@type'           => 'BroadcastEvent',
155
					'isLiveBroadcast' => true,
156
					'startDate'       => $video->live_video_start_date,
157
					'endDate'         => $video->live_video_end_date
158
				);
159
			}
160
161
			$jsonld[] = $single_jsonld;
162
		}
163
164
		return $jsonld;
165
	}
166
167
168
	private function is_associative_array( $arr ) {
169
		if ( array() === $arr ) {
170
			return false;
171
		}
172
173
		return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
174
	}
175
176
177
}
178