| Conditions | 11 |
| Paths | 164 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 ) { |
||
|
|
|||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $video->id = $video->content_url; |
||
| 135 | |||
| 136 | return $video; |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 141 | } |