Completed
Pull Request — master (#284)
by
unknown
05:45
created

NotesApiController::updateData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 4
nop 3
crap 3
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 10
     */
42
    public function __construct($AppName, IRequest $request,
43 10
                                NotesService $service,
44 10
								IUserSession $userSession){
45 10
        parent::__construct($AppName, $request);
46 10
        $this->service = $service;
47
        $this->userSession = $userSession;
48
    }
49
50
51
    /**
52
     * @param Note $note
53
     * @param string[] $exclude the fields that should be removed from the
54
     * notes
55 4
     * @return Note
56 4
     */
57 4
    private function excludeFields(Note $note, array $exclude) {
58 4
        if(count($exclude) > 0) {
59 2
            foreach ($exclude as $field) {
60 2
                if(property_exists($note, $field)) {
61 4
                    unset($note->$field);
62 4
                }
63 4
            }
64
        }
65
        return $note;
66
    }
67
68
69
    /**
70
     * @NoAdminRequired
71
     * @CORS
72
     * @NoCSRFRequired
73
     *
74
     * @param string $exclude
75 2
     * @return DataResponse
76 2
     */
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
        }
83
        return new DataResponse($notes);
84
    }
85
86
87
    /**
88
     * @NoAdminRequired
89
     * @CORS
90
     * @NoCSRFRequired
91
     *
92
     * @param int $id
93
     * @param string $exclude
94 3
     * @return DataResponse
95 3
     */
96
    public function get($id, $exclude='') {
97
        $exclude = explode(',', $exclude);
98 3
99 2 View Code Duplication
        return $this->respond(function () use ($id, $exclude) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100 2
            $note = $this->service->get($id, $this->userSession->getUser()->getUID());
101 3
            $note = $this->excludeFields($note, $exclude);
102
            return $note;
103
        });
104
    }
105
106
107
    /**
108
     * @NoAdminRequired
109
     * @CORS
110
     * @NoCSRFRequired
111
     *
112
     * @param string $content
113 1
     * @param boolean $favorite
114
     * @return DataResponse
115 1
     */
116 1
    public function create($content, $favorite=null) {
117 1 View Code Duplication
        return $this->respond(function () use ($content, $favorite) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
            $note = $this->service->create($this->userSession->getUser()->getUID());
119
            return $this->updateData($note->getId(), $content, $favorite);
120
        });
121
    }
122
123
124
    /**
125
     * @NoAdminRequired
126
     * @CORS
127
     * @NoCSRFRequired
128
     *
129
     * @param int $id
130
     * @param string $content
131 2
     * @param boolean $favorite
132 2
     * @return DataResponse
133
     */
134
    public function update($id, $content=null, $favorite=null) {
135
        return $this->respond(function () use ($id, $content, $favorite) {
136 2
            return $this->updateData($id, $content, $favorite);
137
        });
138
    }
139 2
140
    /**
141 2
     * Updates a note, used by create and update
142
     * @param int $id
143
     * @param string $content
144
     * @param boolean $favorite
145
     * @return Note
146
     */
147
    private function updateData($id, $content, $favorite) {
148
        if($favorite!==null) {
149
            $this->service->favorite($id, $favorite, $this->userSession->getUser()->getUID());
150
        }
151
        if($content===null) {
152
            return $this->service->get($id, $this->userSession->getUser()->getUID());
153
        } else {
154 2
            return $this->service->update($id, $content, $this->userSession->getUser()->getUID());
155 2
        }
156 1
    }
157 2
158
159
    /**
160
     * @NoAdminRequired
161
     * @CORS
162
     * @NoCSRFRequired
163
     *
164
     * @param int $id
165
     * @return DataResponse
166
     */
167
    public function destroy($id) {
168
        return $this->respond(function () use ($id) {
169
            $this->service->delete($id, $this->userSession->getUser()->getUID());
170
            return [];
171
        });
172
    }
173
174
175
}
176