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
|
|
|
$notes = $this->transport->prepare(HttpMethod::GET(), self::URL_NOTES, "")->send(); |
51
|
|
|
if ($notes->code !== 200) { |
52
|
|
|
throw new \HttpException("Something went wrong while fetching notes"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->notes->clear(); |
56
|
|
|
foreach ($notes->body->notes as $note) { |
57
|
|
|
var_dump((array)$note); |
|
|
|
|
58
|
|
|
$this->notes->add(NoteFactory::fromArray((array)$note)); |
59
|
|
|
} |
60
|
|
|
$this->notes->update(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param NoteId $id |
65
|
|
|
* @return Note|null |
66
|
|
|
*/ |
67
|
|
|
public function getOne(NoteId $id) |
68
|
|
|
{ |
69
|
|
|
if ($this->notes->getUpdatedAt() === null) { |
70
|
|
|
$this->fetch(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->notes->find($id)->firstOrNull(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Note $note |
78
|
|
|
* @return NoteId |
79
|
|
|
* @throws \HttpException |
80
|
|
|
*/ |
81
|
|
|
public function saveOne(Note $note) |
82
|
|
|
{ |
83
|
|
|
if ($note->getId() !== null) { |
84
|
|
|
$request = $this->transport->prepare( |
85
|
|
|
HttpMethod::PUT(), |
86
|
|
|
self::URL_NOTES . "/{$note->getId()->getValue()}", |
87
|
|
|
json_encode($note) |
88
|
|
|
); |
89
|
|
|
} else { |
90
|
|
|
$request = $this->transport->prepare( |
91
|
|
|
HttpMethod::POST(), |
92
|
|
|
self::URL_NOTES, |
93
|
|
|
json_encode($note) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
$response = $request->send(); |
97
|
|
|
|
98
|
|
|
if ($response->code > 201 || $response->code < 200) { |
99
|
|
|
throw new \HttpException("Something went wrong while saving note"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new NoteId($response->body->id); |
103
|
|
|
} |
104
|
|
|
} |