1 | <?php |
||
11 | class Client |
||
12 | { |
||
13 | protected $token; |
||
14 | |||
15 | protected $organizationId; |
||
16 | |||
17 | protected $cache; |
||
18 | |||
19 | protected $client; |
||
20 | |||
21 | protected $ttl; |
||
22 | |||
23 | protected $messageFactory; |
||
24 | |||
25 | public function __construct(string $token, int $organizationId, Cache $cache, int $ttl = 7200) |
||
26 | { |
||
27 | $this->token = $token; |
||
28 | $this->organizationId = $organizationId; |
||
29 | $this->ttl = $ttl; |
||
30 | $this->cache = $cache; |
||
31 | $this->client = HttpClientDiscovery::find(); |
||
32 | $this->messageFactory = MessageFactoryDiscovery::find(); |
||
33 | } |
||
34 | |||
35 | public function getFromCache(string $key) |
||
36 | { |
||
37 | // If the results are already cached |
||
38 | if ($this->cache->contains($key)) { |
||
39 | return unserialize($this->cache->fetch($key)); |
||
40 | } |
||
41 | |||
42 | return false; |
||
43 | } |
||
44 | |||
45 | public function saveToCache(string $key, $values): bool |
||
46 | { |
||
47 | return $this->cache->save($key, serialize($values), $this->ttl); |
||
48 | } |
||
49 | |||
50 | public function deleteCacheByKey(string $key) |
||
54 | |||
55 | protected function processResponse(ResponseInterface $response): array |
||
56 | { |
||
57 | $data = json_decode($response->getBody()->getContents(), true); |
||
58 | |||
59 | if ($data['code'] != 0) { |
||
60 | throw new \Exception('Zoho Api subscription error : '.$data['message']); |
||
61 | } |
||
62 | |||
63 | return $data; |
||
64 | } |
||
65 | |||
66 | protected function sendRequest(string $method, string $uri, array $headers = [], string $body = null) |
||
73 | |||
74 | protected function getRequestHeaders(array $headers = []) |
||
83 | } |
||
84 |