Completed
Push — master ( b65164...009680 )
by Albert
08:03
created

EnvatoApi::buildHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
namespace Elimuswift\Core;
3
4
class EnvatoApi {
5
6
	/**
7
	 * Envanto API bearer token
8
	 *
9
	 * @var string
10
	 **/
11
	private $bearer;
12
	
13
	/**
14
	 * Purchase verification endpoint
15
	 *
16
	 * @var string
17
	 **/
18
	protected $url = 'https://api.envato.com/v3/market/author/sale?code=';
19
20
	/** 
21
	 * Request headers
22
	 *
23
	 * @var array
24
	 **/
25
	private $headers;
26
27
	/**
28
	 * Create EnvantoApi Instance
29
	 * 
30
	 **/
31
	public function __construct($bearer)
32
	{
33
		$this->bearer = $bearer;
34
		$this->buildHeaders();
35
	}
36
37
	/**
38
	 * Make a call to the Envato API to verify purchase
39
	 *
40
	 * @return string Guzzle\Response::getBody()
41
	 * @param string $code 
42
	 **/
43
	public function getPurchaseData( $code ) 
44
	{
45
		$ch_verify = curl_init( $this->url. $code );
46
	    curl_setopt( $ch_verify, CURLOPT_HTTPHEADER, $this->headers );
47
	    curl_setopt( $ch_verify, CURLOPT_SSL_VERIFYPEER, false );
48
	    curl_setopt( $ch_verify, CURLOPT_RETURNTRANSFER, 1 );
49
	    curl_setopt( $ch_verify, CURLOPT_CONNECTTIMEOUT, 5 );
50
	    curl_setopt( $ch_verify, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
51
	    
52
	    $cinit_verify_data = curl_exec( $ch_verify );
53
	    curl_close( $ch_verify );
54
	    
55
	    if ($cinit_verify_data != "")    
56
	      return json_decode($cinit_verify_data, true); 
57
58
	  	return ['error' => 'exception', 'description' => 'A server error was encountered please notify us if you see this']; 
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('error' => ...y us if you see this'); (array<string,string>) is incompatible with the return type documented by Elimuswift\Core\EnvatoApi::getPurchaseData 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...
59
		     
60
	}
61
  
62
	/**
63
	 * Verify purchase 
64
	 *
65
	 * @return mixed Array 
66
	 * @param string $code Purchase Code 
67
	 **/
68
	public function verifyPurchase(string $code ) 
69
	{
70
		$purchase = [];
71
	    $purchase['response'] = (object) $this->getPurchaseData($code); 
72
	    if($purchase->error)
73
	    	return $purchase['status'] = 'error';
74
	    else
75
	    	return $purchase['status'] = 'success';
76
	}
77
78
	/**
79
	 * setting the header for the rest of the api
80
	 *
81
	 * @return void
82
	 * 
83
	 **/
84
	protected function buildHeaders()
85
	{
86
	    $headers   = [
87
	    	'Content-type' => 'application/json',
88
			'Authorization' =>  'Bearer '.$this->bearer
89
		];
90
		$h = [];
91
		foreach ($headers as $key => $value) {
92
			$h[] = $key.':'.$value;
93
		}
94
		$this->headers = $h;
95
96
	}
97
}