1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2015 Dirk Groenen |
4
|
|
|
* |
5
|
|
|
* (c) Dirk Groenen <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DirkGroenen\Pinterest; |
12
|
|
|
|
13
|
|
|
use DirkGroenen\Pinterest\Auth\PinterestOAuth; |
14
|
|
|
use DirkGroenen\Pinterest\Utils\CurlBuilder; |
15
|
|
|
use DirkGroenen\Pinterest\Transport\Request; |
16
|
|
|
use DirkGroenen\Pinterest\Exceptions\InvalidEndpointException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Boards boards |
20
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Following following |
21
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Pins pins |
22
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Users users |
23
|
|
|
* @property \DirkGroenen\Pinterest\Endpoints\Sections sections |
24
|
|
|
*/ |
25
|
|
|
class Pinterest { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Reference to authentication class instance |
29
|
|
|
* |
30
|
|
|
* @var Auth\PinterestOAuth |
31
|
|
|
*/ |
32
|
|
|
public $auth; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A reference to the request class which travels |
36
|
|
|
* through the application |
37
|
|
|
* |
38
|
|
|
* @var Transport\Request |
39
|
|
|
*/ |
40
|
|
|
public $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A array containing the cached endpoints |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $cachedEndpoints = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor |
51
|
|
|
* |
52
|
|
|
* @param string $client_id |
53
|
|
|
* @param string $client_secret |
54
|
|
|
* @param CurlBuilder $curlbuilder |
55
|
|
|
*/ |
56
|
43 |
|
public function __construct($client_id, $client_secret, $curlbuilder = null) |
57
|
|
|
{ |
58
|
43 |
|
if ($curlbuilder == null) { |
59
|
|
|
$curlbuilder = new CurlBuilder(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Create new instance of Transport\Request |
63
|
43 |
|
$this->request = new Request($curlbuilder); |
64
|
|
|
|
65
|
|
|
// Create and set new instance of the OAuth class |
66
|
43 |
|
$this->auth = new PinterestOAuth($client_id, $client_secret, $this->request); |
67
|
43 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get an Pinterest API endpoint |
71
|
|
|
* |
72
|
|
|
* @access public |
73
|
|
|
* @param string $endpoint |
74
|
|
|
* @return mixed |
75
|
|
|
* @throws Exceptions\InvalidEndpointException |
76
|
|
|
*/ |
77
|
39 |
|
public function __get($endpoint) |
78
|
|
|
{ |
79
|
39 |
|
$endpoint = strtolower($endpoint); |
80
|
39 |
|
$class = "\\DirkGroenen\\Pinterest\\Endpoints\\" . ucfirst($endpoint); |
81
|
|
|
|
82
|
|
|
// Check if an instance has already been initiated |
83
|
39 |
|
if (!isset($this->cachedEndpoints[$endpoint])) { |
84
|
|
|
// Check endpoint existence |
85
|
39 |
|
if (!class_exists($class)) { |
86
|
|
|
throw new InvalidEndpointException; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Create a reflection of the called class and initialize it |
90
|
|
|
// with a reference to the request class |
91
|
39 |
|
$ref = new \ReflectionClass($class); |
92
|
39 |
|
$obj = $ref->newInstanceArgs([$this->request, $this]); |
93
|
|
|
|
94
|
39 |
|
$this->cachedEndpoints[$endpoint] = $obj; |
95
|
|
|
} |
96
|
|
|
|
97
|
39 |
|
return $this->cachedEndpoints[$endpoint]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get rate limit from the headers |
102
|
|
|
* response header may change from X-Ratelimit-Limit to X-RateLimit-Limit |
103
|
|
|
* @access public |
104
|
|
|
* @return integer |
105
|
|
|
*/ |
106
|
1 |
View Code Duplication |
public function getRateLimit() |
|
|
|
|
107
|
|
|
{ |
108
|
1 |
|
$header = $this->request->getHeaders(); |
109
|
1 |
|
if (is_array($header)) { |
110
|
|
|
$header = array_change_key_case($header, CASE_LOWER); |
111
|
|
|
} |
112
|
1 |
|
return (isset($header['x-ratelimit-limit']) ? $header['x-ratelimit-limit'] : 1000); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get rate limit remaining from the headers |
117
|
|
|
* response header may change from X-Ratelimit-Remaining to X-RateLimit-Remaining |
118
|
|
|
* @access public |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
1 |
View Code Duplication |
public function getRateLimitRemaining() |
|
|
|
|
122
|
|
|
{ |
123
|
1 |
|
$header = $this->request->getHeaders(); |
124
|
1 |
|
if (is_array($header)) { |
125
|
|
|
$header = array_change_key_case($header, CASE_LOWER); |
126
|
|
|
} |
127
|
1 |
|
return (isset($header['x-ratelimit-remaining']) ? $header['x-ratelimit-remaining'] : 'unknown'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.