|
1
|
|
|
<?php |
|
2
|
|
|
namespace Barenote\Endpoint; |
|
3
|
|
|
|
|
4
|
|
|
use Barenote\Collection\NoteCollection; |
|
5
|
|
|
use Barenote\Domain\Identity\NoteId; |
|
6
|
|
|
use Barenote\Domain\Note; |
|
7
|
|
|
use Barenote\Enum\HttpMethod; |
|
8
|
|
|
use Barenote\Exception\NotAuthenticated; |
|
9
|
|
|
use Barenote\Factory\NoteFactory; |
|
10
|
|
|
use Barenote\Transport\Transport; |
|
11
|
|
|
|
|
12
|
|
|
class Notes |
|
13
|
|
|
{ |
|
14
|
|
|
const URL_NOTES = '/api/note'; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Transport |
|
17
|
|
|
*/ |
|
18
|
|
|
private $transport; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var NoteCollection |
|
22
|
|
|
*/ |
|
23
|
|
|
private $notes; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(Transport $transport) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->transport = $transport; |
|
28
|
|
|
$this->notes = new NoteCollection(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getAll() |
|
32
|
|
|
{ |
|
33
|
|
|
if ($this->notes->getUpdatedAt() === null) { |
|
34
|
|
|
$this->fetch(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this->notes; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Fetch most recent data to cache |
|
42
|
|
|
* @throws NotAuthenticated |
|
43
|
|
|
* @throws \HttpException |
|
44
|
|
|
*/ |
|
45
|
|
|
private function fetch() |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$this->transport->isAuthenticated()) { |
|
48
|
|
|
throw new NotAuthenticated("You must be authenticated to fetch notes"); |
|
49
|
|
|
} |
|
50
|
|
|
$response = $this->transport->prepare(HttpMethod::GET(), self::URL_NOTES, "")->send(); |
|
51
|
|
|
if ($response->code !== 200) { |
|
52
|
|
|
throw new \HttpException("Something went wrong while fetching notes"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->notes->clear(); |
|
56
|
|
|
foreach ($response->body->notes as $note) { |
|
57
|
|
|
$this->notes->add(NoteFactory::fromArray((array)$note)); |
|
58
|
|
|
} |
|
59
|
|
|
$this->notes->update(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param NoteId $id |
|
64
|
|
|
* @return Note|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getOne(NoteId $id) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getAll()->find($id)->firstOrNull(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Update existing note |
|
73
|
|
|
* @param Note $note |
|
74
|
|
|
* @return void |
|
75
|
|
|
* @throws \HttpException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function update(Note $note) |
|
78
|
|
|
{ |
|
79
|
|
|
$request = $this->transport->prepare( |
|
80
|
|
|
HttpMethod::PUT(), |
|
81
|
|
|
self::URL_NOTES . "/{$note->getId()->getValue()}", |
|
82
|
|
|
json_encode($note) |
|
83
|
|
|
); |
|
84
|
|
|
$response = $request->send(); |
|
85
|
|
|
|
|
86
|
|
|
if ($response->code !== 200) { |
|
87
|
|
|
throw new \HttpException("Something went wrong while saving note"); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Insert a new note to the system |
|
93
|
|
|
* @param Note $note |
|
94
|
|
|
* @return NoteId |
|
95
|
|
|
* @throws \HttpException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function insert(Note $note) |
|
98
|
|
|
{ |
|
99
|
|
|
$request = $this->transport->prepare( |
|
100
|
|
|
HttpMethod::POST(), |
|
101
|
|
|
self::URL_NOTES, |
|
102
|
|
|
json_encode($note) |
|
103
|
|
|
); |
|
104
|
|
|
$response = $request->send(); |
|
105
|
|
|
|
|
106
|
|
|
if ($response->code !== 201) { |
|
107
|
|
|
throw new \HttpException("Something went wrong while saving new note."); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return new NoteId($response->body->id); |
|
111
|
|
|
} |
|
112
|
|
|
} |