Completed
Pull Request — master (#7)
by Michał
03:23 queued 01:29
created

Notes   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 93
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAll() 0 8 2
A fetch() 0 17 4
A getOne() 0 8 2
B saveOne() 0 23 4
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);
0 ignored issues
show
Security Debugging Code introduced by
var_dump((array) $note); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
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
}