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

Vimeo_Client::get_api_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Videoobject\Provider\Client;
4
5
use Wordlift\Common\Singleton;
6
7
/**
8
 * @since 3.31.0
9
 * @author Naveen Muthusamy <[email protected]>
10
 * This class acts as api client for vimeo.
11
 */
12
class Vimeo_Client extends Singleton implements Client {
13
14
	static $requests_sent = 0;
15
16
	const VIMEO_URL_REGEX = '/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/';
17
18
	/**
19
	 * @param $url string Vimeo url
20
	 */
21
	public function vimeo_url_to_id( $url ) {
22
		if ( ! $url ) {
23
			return false;
24
		}
25
		preg_match( self::VIMEO_URL_REGEX, $url, $matches );
26
27
		if ( ! array_key_exists( 3, $matches ) ) {
28
			return false;
29
		}
30
31
		return '/videos/' . $matches[3];
32
	}
33
34
	/**
35
	 * @param $vimeo_urls
36
	 * @param $post_id
37
	 *
38
	 * @return string[]
39
	 */
40
	public function get_video_ids_for_api( $vimeo_urls ) {
41
42
		return array_filter( array_map( array( $this, 'vimeo_url_to_id' ), $vimeo_urls ) );
43
	}
44
45
46
	public function get_data( $video_urls ) {
47
		$ids = join( ",", $this->get_video_ids_for_api( $video_urls ) );
48
49
		if ( ! $ids ) {
50
			return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Wordlift\Videoobject\Pro...Client\Client::get_data of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
51
		}
52
53
		$api_url = $this->get_api_url() . "/videos/";
54
		$api_url = add_query_arg( array(
55
			'uris'   => $ids,
56
			'fields' => 'name,description,link,uri,duration,release_time,pictures,stats'
57
		), $api_url );
58
59
60
		$response = wp_remote_get( $api_url, array(
61
			'headers' => array(
62
				'Authorization' => 'bearer ' . $this->get_api_key()
63
			)
64
		) );
65
66
		self::$requests_sent += 1;
67
68
		return wp_remote_retrieve_body( $response );
69
	}
70
71
	public static function get_api_key() {
72
		return get_option( self::get_api_key_option_name(), false );
73
	}
74
75
	public static function get_api_key_option_name() {
76
		return '_wl_videoobject_vimeo_api_key';
77
	}
78
79
	public function get_api_url() {
80
		return 'https://api.vimeo.com';
81
	}
82
}