ItemApiController::updated()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Controller;
15
16
use \OCP\IRequest;
17
use \OCP\AppFramework\ApiController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\News\Controller\ApiController.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use \OCP\AppFramework\Http;
19
20
use \OCA\News\Service\ItemService;
21
use \OCA\News\Service\ServiceNotFoundException;
22
23
class ItemApiController extends ApiController {
24
25
    use JSONHttpError;
26
27
    private $itemService;
28
    private $userId;
29
    private $serializer;
30
31
    public function __construct($AppName,
32
                                IRequest $request,
33
                                ItemService $itemService,
34
                                $UserId){
35
        parent::__construct($AppName, $request);
36
        $this->itemService = $itemService;
37
        $this->userId = $UserId;
38
        $this->serializer = new EntityApiSerializer('items');
39
    }
40
41
42
    /**
43
     * @NoAdminRequired
44
     * @NoCSRFRequired
45
     * @CORS
46
     *
47
     * @param int $type
48
     * @param int $id
49
     * @param bool $getRead
50
     * @param int $batchSize
51
     * @param int $offset
52
     * @param bool $oldestFirst
53
     * @return array|mixed
54
     */
55
    public function index($type=3, $id=0, $getRead=true, $batchSize=-1,
56
                          $offset=0, $oldestFirst=false) {
57
        return $this->serializer->serialize(
58
            $this->itemService->findAll(
59
                $id, $type, $batchSize, $offset, $getRead, $oldestFirst,
60
                $this->userId
61
            )
62
        );
63
    }
64
65
66
    /**
67
     * @NoAdminRequired
68
     * @NoCSRFRequired
69
     * @CORS
70
     *
71
     * @param int $type
72
     * @param int $id
73
     * @param int $lastModified
74
     * @return array|mixed
75
     */
76
    public function updated($type=3, $id=0, $lastModified=0) {
77
        return $this->serializer->serialize(
78
            $this->itemService->findAllNew($id, $type, $lastModified,
79
                                           true, $this->userId)
80
        );
81
    }
82
83
84
    private function setRead($isRead, $itemId) {
85
        try {
86
            $this->itemService->read($itemId, $isRead, $this->userId);
87
        } catch(ServiceNotFoundException $ex){
88
            return $this->error($ex, Http::STATUS_NOT_FOUND);
89
        }
90
91
        return [];
92
    }
93
94
95
    /**
96
     * @NoAdminRequired
97
     * @NoCSRFRequired
98
     * @CORS
99
     *
100
     * @param int $itemId
101
     * @return array|\OCP\AppFramework\Http\JSONResponse
102
     */
103
    public function read($itemId) {
104
        return $this->setRead(true, $itemId);
105
    }
106
107
108
    /**
109
     * @NoAdminRequired
110
     * @NoCSRFRequired
111
     * @CORS
112
     *
113
     * @param int $itemId
114
     * @return array|\OCP\AppFramework\Http\JSONResponse
115
     */
116
    public function unread($itemId) {
117
        return $this->setRead(false, $itemId);
118
    }
119
120
121
    private function setStarred($isStarred, $feedId, $guidHash) {
122
        try {
123
            $this->itemService->star(
124
                $feedId, $guidHash, $isStarred, $this->userId
125
            );
126
        } catch(ServiceNotFoundException $ex){
127
            return $this->error($ex, Http::STATUS_NOT_FOUND);
128
        }
129
130
        return [];
131
    }
132
133
134
    /**
135
     * @NoAdminRequired
136
     * @NoCSRFRequired
137
     * @CORS
138
     *
139
     * @param int $feedId
140
     * @param string $guidHash
141
     * @return array|\OCP\AppFramework\Http\JSONResponse
142
     */
143
    public function star($feedId, $guidHash) {
144
        return $this->setStarred(true, $feedId, $guidHash);
145
    }
146
147
148
    /**
149
     * @NoAdminRequired
150
     * @NoCSRFRequired
151
     * @CORS
152
     *
153
     * @param int $feedId
154
     * @param string $guidHash
155
     * @return array|\OCP\AppFramework\Http\JSONResponse
156
     */
157
    public function unstar($feedId, $guidHash) {
158
        return $this->setStarred(false, $feedId, $guidHash);
159
    }
160
161
162
    /**
163
     * @NoAdminRequired
164
     * @NoCSRFRequired
165
     * @CORS
166
     *
167
     * @param int $newestItemId
168
     */
169
    public function readAll($newestItemId) {
170
        $this->itemService->readAll($newestItemId, $this->userId);
171
    }
172
173
174
    private function setMultipleRead($isRead, $items) {
175
        foreach($items as $id) {
176
            try {
177
                $this->itemService->read($id, $isRead, $this->userId);
178
            } catch(ServiceNotFoundException $ex) {
179
                continue;
180
            }
181
        }
182
    }
183
184
185
    /**
186
     * @NoAdminRequired
187
     * @NoCSRFRequired
188
     * @CORS
189
     *
190
     * @param int[] item ids
191
     */
192
    public function readMultiple($items) {
193
        $this->setMultipleRead(true, $items);
194
    }
195
196
197
    /**
198
     * @NoAdminRequired
199
     * @NoCSRFRequired
200
     * @CORS
201
     *
202
     * @param int[] item ids
203
     */
204
    public function unreadMultiple($items) {
205
        $this->setMultipleRead(false, $items);
206
    }
207
208
209
    private function setMultipleStarred($isStarred, $items) {
210
        foreach($items as $item) {
211
            try {
212
                $this->itemService->star($item['feedId'], $item['guidHash'],
213
                                               $isStarred, $this->userId);
214
            } catch(ServiceNotFoundException $ex) {
215
                continue;
216
            }
217
        }
218
    }
219
220
221
    /**
222
     * @NoAdminRequired
223
     * @NoCSRFRequired
224
     * @CORS
225
     *
226
     * @param int[] item ids
227
     */
228
    public function starMultiple($items) {
229
        $this->setMultipleStarred(true, $items);
230
    }
231
232
233
    /**
234
     * @NoAdminRequired
235
     * @NoCSRFRequired
236
     * @CORS
237
     *
238
     * @param int[] item ids
239
     */
240
    public function unstarMultiple($items) {
241
        $this->setMultipleStarred(false, $items);
242
    }
243
244
245
}
246