1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CryptoPete\Frost; |
4
|
|
|
|
5
|
|
|
use CryptoPete\Frost\Adapter\Http\HttpInterface; |
6
|
|
|
use CryptoPete\Frost\Adapter\Settings\SettingsInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FrostController |
10
|
|
|
* |
11
|
|
|
* Access the various API methods in the Frost API. |
12
|
|
|
* |
13
|
|
|
* See https://docs.frost.po.et/v0.1/reference for more details |
14
|
|
|
*/ |
15
|
|
|
class FrostController |
16
|
|
|
{ |
17
|
|
|
const FrostApi = 'https://api.frost.po.et/works'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var SettingsInterface |
21
|
|
|
*/ |
22
|
|
|
protected $settings; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var HttpInterface |
26
|
|
|
*/ |
27
|
|
|
protected $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* FrostController constructor. |
31
|
|
|
* |
32
|
|
|
* @param SettingsInterface $settings |
33
|
|
|
* @param HttpInterface $client |
34
|
|
|
*/ |
35
|
6 |
|
public function __construct(SettingsInterface $settings, HttpInterface $client) |
36
|
|
|
{ |
37
|
6 |
|
$this->settings = $settings; |
38
|
6 |
|
$this->client = $client; |
39
|
6 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new work in the Po.et ecosystem |
43
|
|
|
* |
44
|
|
|
* @param array $work The array of the work details |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
2 |
|
public function createWork(array $work): array |
49
|
|
|
{ |
50
|
2 |
|
return $this->toArray($this->client->post(self::FrostApi, $this->settings->apiKey(), $work)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Fetch an individual work by passing the unique work id |
55
|
|
|
* |
56
|
|
|
* @param string|null $id Optional work id |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
2 |
|
public function getWorkById(string $id = null): array |
61
|
|
|
{ |
62
|
2 |
|
return $this->toArray($this->client->get(self::FrostApi . "/$id", $this->settings->apiKey())); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Fetch all the works |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
1 |
|
public function getWorks(): array |
71
|
|
|
{ |
72
|
1 |
|
return $this->getWorkById(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return an array for all valid responses |
77
|
|
|
* |
78
|
|
|
* @param string $string The json response string |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
2 |
|
protected function toArray(string $string): array |
83
|
|
|
{ |
84
|
2 |
|
return json_decode($string, true); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|