-
Notifications
You must be signed in to change notification settings - Fork 31
/
FeedlyModel.php
80 lines (61 loc) · 1.73 KB
/
FeedlyModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace feedly\Models;
use feedly\AccessTokenStorage\AccessTokenStorage;
use feedly\HTTPClient;
use feedly\Mode\Mode;
use \Exception;
use \RuntimeException;
abstract class FeedlyModel
{
private $options;
private $client;
/**
* @var Mode
*/
private $apiMode;
public function __construct(Mode $apiMode, AccessTokenStorage $accessTokenStorage)
{
$this->apiMode = $apiMode;
$this->client = new HTTPClient();
$this->getClient()
->setCustomHeader(array(
"Authorization: OAuth " . $accessTokenStorage->getAccessToken(),
'Content-Type: application/json'
));
}
public function fetch()
{
if (!is_string($this->getEndpoint()))
throw new RuntimeException('An endpoint must be set');
return $this->client->get($this->getApiBaseUrl() . $this->getEndpoint());
}
public function persist()
{
if (!is_string($this->getEndpoint()))
throw new \RuntimeException('An endpoint must be set');
return $this->client->post($this->getApiBaseUrl() . $this->getEndpoint());
}
public function setOptions($options)
{
$this->options = $options;
$this->client->setPostParams($this->options);
return $this;
}
public function getOptions()
{
return $this->options;
}
public function setClient($client)
{
$this->client = $client;
}
public function getClient()
{
return $this->client;
}
public function getApiBaseUrl()
{
return $this->apiMode->getApiBaseUrl();
}
abstract public function getEndpoint();
}