|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mechpave\ImgurClient; |
|
4
|
|
|
|
|
5
|
|
|
use Mechpave\ImgurClient\Api\Album; |
|
6
|
|
|
use Mechpave\ImgurClient\Api\Image; |
|
7
|
|
|
use Mechpave\ImgurClient\Authorization\AuthorizationHandlerInterface; |
|
8
|
|
|
use Mechpave\ImgurClient\Entity\TokenInterface; |
|
9
|
|
|
use Symfony\Component\Config\FileLocator; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ImgurClient |
|
17
|
|
|
* @package Mechpave\ImgurClient |
|
18
|
|
|
*/ |
|
19
|
|
|
class ImgurClient |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Client ID, received after registering Imgur Application |
|
23
|
|
|
* |
|
24
|
|
|
* @see https://api.imgur.com/oauth2/addclient |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $clientId; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Client Secret token, received after registering Imgur Application |
|
31
|
|
|
* |
|
32
|
|
|
* @see https://api.imgur.com/oauth2/addclient |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $clientSecret; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Mashape key (used for commercial products) |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $mashapeKey; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Object, containing Imgur access token, which is used to access users private data |
|
46
|
|
|
* |
|
47
|
|
|
* @var TokenInterface; |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $token; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Service container |
|
53
|
|
|
* |
|
54
|
|
|
* @var Container |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $container; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Client constructor. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $clientId |
|
62
|
|
|
* @param string $clientSecret |
|
63
|
|
|
* @param string $mashapeKey |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
$clientId, |
|
67
|
|
|
$clientSecret = null, |
|
68
|
|
|
$mashapeKey = null |
|
69
|
|
|
) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->setClientId($clientId); |
|
72
|
|
|
$this->setClientSecret($clientSecret); |
|
73
|
|
|
$this->setMashapeKey($mashapeKey); |
|
74
|
|
|
|
|
75
|
|
|
//initialize Dependency Injection service container |
|
76
|
|
|
$this->initializeContainer(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set client id |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $clientId |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setClientId($clientId) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->clientId = $clientId; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get client Id |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getClientId() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->clientId; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set client secret |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $clientSecret |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setClientSecret($clientSecret) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->clientSecret = $clientSecret; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get client secret |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getClientSecret() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->clientSecret; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set Mashape key |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $mashapeKey |
|
129
|
|
|
* @return $this |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setMashapeKey($mashapeKey) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->mashapeKey = $mashapeKey; |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get Mashape key |
|
140
|
|
|
* |
|
141
|
|
|
* @return mixed |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getMashapeKey() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->mashapeKey; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param TokenInterface $token |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setToken(TokenInterface $token) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->token = $token; |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return TokenInterface |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getToken() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->token; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get authorization handler |
|
169
|
|
|
* |
|
170
|
|
|
* @return AuthorizationHandlerInterface |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getAuthorizationHandler() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->container->get('authorization_handler'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Image API endpoint |
|
179
|
|
|
* |
|
180
|
|
|
* @see Mechpave\ImgurClient\Api\Image |
|
181
|
|
|
* @return Image |
|
182
|
|
|
*/ |
|
183
|
|
|
public function image() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->container->get('image_api'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Album API endpoint |
|
190
|
|
|
* |
|
191
|
|
|
* @see Mechpave\ImgurClient\Api\Image |
|
192
|
|
|
* @return Album |
|
193
|
|
|
*/ |
|
194
|
|
|
public function album() |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->container->get('album_api'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Initializes service container |
|
201
|
|
|
* @see http://symfony.com/doc/current/components/dependency_injection/index.html |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function initializeContainer() |
|
204
|
|
|
{ |
|
205
|
|
|
$container = new ContainerBuilder(); |
|
206
|
|
|
|
|
207
|
|
|
$clientDefinition = new Definition(); |
|
208
|
|
|
$clientDefinition->setSynthetic(true); |
|
209
|
|
|
$container->setDefinition('client', $clientDefinition); |
|
210
|
|
|
$container->set('client', $this); |
|
211
|
|
|
|
|
212
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/Resources/config')); |
|
213
|
|
|
$loader->load('services.yml'); |
|
214
|
|
|
$loader->load('api_services.yml'); |
|
215
|
|
|
$loader->load('mapper_services.yml'); |
|
216
|
|
|
|
|
217
|
|
|
$this->container = $container; |
|
218
|
|
|
} |
|
219
|
|
|
} |