Completed
Pull Request — master (#268)
by
unknown
42:27 queued 40:45
created

NotesApiController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.02%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 137
ccs 40
cts 43
cp 0.9302
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A excludeFields() 0 10 4
A index() 0 8 2
A get() 0 9 1
A create() 0 6 1
A update() 0 12 3
A destroy() 0 6 1
1
<?php
2
/**
3
 * ownCloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Controller;
13
14
use OCP\AppFramework\ApiController;
15
use OCP\AppFramework\Http\DataResponse;
16
use OCP\IRequest;
17
18
use OCA\Notes\Service\NotesService;
19
use OCA\Notes\Db\Note;
20
use OCP\IUserSession;
21
22
/**
23
 * Class NotesApiController
24
 *
25
 * @package OCA\Notes\Controller
26
 */
27
class NotesApiController extends ApiController {
28
29
    use Errors;
30
31
    /** @var NotesService */
32
    private $service;
33
    /** @var IUserSession */
34
    private $userSession;
35
36
    /**
37
     * @param string $AppName
38
     * @param IRequest $request
39
     * @param NotesService $service
40
     * @param IUserSession $userSession
41
     */
42 10
    public function __construct($AppName, IRequest $request,
43
                                NotesService $service,
44
								IUserSession $userSession){
45 10
        parent::__construct($AppName, $request);
46 10
        $this->service = $service;
47 10
        $this->userSession = $userSession;
48 10
    }
49
50
51
    /**
52
     * @param Note $note
53
     * @param string[] $exclude the fields that should be removed from the
54
     * notes
55
     * @return Note
56
     */
57 4
    private function excludeFields(Note $note, array $exclude) {
58 4
        if(count($exclude) > 0) {
59 4
            foreach ($exclude as $field) {
60 4
                if(property_exists($note, $field)) {
61 2
                    unset($note->$field);
62 2
                }
63 4
            }
64 4
        }
65 4
        return $note;
66
    }
67
68
69
    /**
70
     * @NoAdminRequired
71
     * @CORS
72
     * @NoCSRFRequired
73
     *
74
     * @param string $exclude
75
     * @return DataResponse
76
     */
77 2
    public function index($exclude='') {
78 2
        $exclude = explode(',', $exclude);
79 2
        $notes = $this->service->getAll($this->userSession->getUser()->getUID());
80 2
        foreach ($notes as $note) {
81 2
            $note = $this->excludeFields($note, $exclude);
82 2
        }
83 2
        return new DataResponse($notes);
84
    }
85
86
87
    /**
88
     * @NoAdminRequired
89
     * @CORS
90
     * @NoCSRFRequired
91
     *
92
     * @param int $id
93
     * @param string $exclude
94
     * @return DataResponse
95
     */
96 3
    public function get($id, $exclude='') {
97 3
        $exclude = explode(',', $exclude);
98
99
        return $this->respond(function () use ($id, $exclude) {
100 3
            $note = $this->service->get($id, $this->userSession->getUser()->getUID());
101 2
            $note = $this->excludeFields($note, $exclude);
102 2
            return $note;
103 3
        });
104
    }
105
106
107
    /**
108
     * @NoAdminRequired
109
     * @CORS
110
     * @NoCSRFRequired
111
     *
112
     * @param string $content
113
     * @return DataResponse
114
     */
115 1
    public function create($content) {
116
        return $this->respond(function () use ($content) {
117 1
            $note = $this->service->create($this->userSession->getUser()->getUID());
118 1
            return $this->service->update($note->getId(), $content, $this->userSession->getUser()->getUID());
119 1
        });
120
    }
121
122
123
    /**
124
     * @NoAdminRequired
125
     * @CORS
126
     * @NoCSRFRequired
127
     *
128
     * @param int $id
129
     * @param string $content
130
     * @param boolean $favorite
131
     * @return DataResponse
132
     */
133 2
    public function update($id, $content=null, $favorite=null) {
134 2
        if($favorite!==null) {
135
            $this->service->favorite($id, $favorite, $this->userSession->getUser()->getUID());
136
        }
137
        return $this->respond(function () use ($id, $content) {
138 2
            if($content===null) {
139
                return $this->service->get($id, $this->userSession->getUser()->getUID());
140
            } else {
141 2
                return $this->service->update($id, $content, $this->userSession->getUser()->getUID());
142
            }
143 2
        });
144
    }
145
146
147
    /**
148
     * @NoAdminRequired
149
     * @CORS
150
     * @NoCSRFRequired
151
     *
152
     * @param int $id
153
     * @return DataResponse
154
     */
155
    public function destroy($id) {
156 2
        return $this->respond(function () use ($id) {
157 2
            $this->service->delete($id, $this->userSession->getUser()->getUID());
158 1
            return [];
159 2
        });
160
    }
161
162
163
}
164