Completed
Pull Request — master (#7)
by Michał
02:49 queued 51s
created

Notes   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 92
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 16 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
            $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
        if ($this->notes->getUpdatedAt() === null) {
69
            $this->fetch();
70
        }
71
72
        return $this->notes->find($id)->firstOrNull();
73
    }
74
75
    /**
76
     * @param Note $note
77
     * @return NoteId
78
     * @throws \HttpException
79
     */
80
    public function saveOne(Note $note)
81
    {
82
        if ($note->getId() !== null) {
83
            $request = $this->transport->prepare(
84
                HttpMethod::PUT(),
85
                self::URL_NOTES . "/{$note->getId()->getValue()}",
86
                json_encode($note)
87
            );
88
        } else {
89
            $request = $this->transport->prepare(
90
                HttpMethod::POST(),
91
                self::URL_NOTES,
92
                json_encode($note)
93
            );
94
        }
95
        $response = $request->send();
96
97
        if ($response->code > 201 || $response->code < 200) {
98
            throw new \HttpException("Something went wrong while saving note");
99
        }
100
101
        return new NoteId($response->body->id);
102
    }
103
}