FeedController   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 6
dl 0
loc 273
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A index() 0 20 2
B active() 0 36 6
B create() 0 29 4
A delete() 0 9 2
A update() 0 20 2
A import() 0 13 2
A read() 0 12 1
A restore() 0 9 2
B patch() 0 24 2
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\IConfig;
18
use OCP\AppFramework\Controller;
19
use OCP\AppFramework\Http;
20
21
use OCA\News\Service\ItemService;
22
use OCA\News\Service\FeedService;
23
use OCA\News\Service\FolderService;
24
use OCA\News\Service\ServiceNotFoundException;
25
use OCA\News\Service\ServiceConflictException;
26
use OCA\News\Db\FeedType;
27
28
29
class FeedController extends Controller {
30
31
    use JSONHttpError;
32
33
    private $feedService;
34
    private $folderService;
35
    private $itemService;
36
    private $userId;
37
    private $settings;
38
39
    public function __construct($AppName,
40
                                IRequest $request,
41
                                FolderService $folderService,
42
                                FeedService $feedService,
43
                                ItemService $itemService,
44
                                IConfig $settings,
45
                                $UserId){
46
        parent::__construct($AppName, $request);
47
        $this->feedService = $feedService;
48
        $this->folderService = $folderService;
49
        $this->itemService = $itemService;
50
        $this->userId = $UserId;
51
        $this->settings = $settings;
52
    }
53
54
55
    /**
56
     * @NoAdminRequired
57
     */
58
    public function index(){
59
60
        // this method is also used to update the interface
61
        // because of this we also pass the starred count and the newest
62
        // item id which will be used for marking feeds read
63
        $params = [
64
            'feeds' => $this->feedService->findAll($this->userId),
65
            'starred' => $this->itemService->starredCount($this->userId)
66
        ];
67
68
        try {
69
            $params['newestItemId'] =
70
                $this->itemService->getNewestItemId($this->userId);
71
72
        // An exception occurs if there is a newest item. If there is none,
73
        // simply ignore it and do not add the newestItemId
74
        } catch (ServiceNotFoundException $ex) {}
75
76
        return $params;
77
    }
78
79
80
    /**
81
     * @NoAdminRequired
82
     */
83
    public function active(){
84
        $feedId = (int) $this->settings->getUserValue($this->userId,
85
            $this->appName,'lastViewedFeedId');
86
        $feedType = $this->settings->getUserValue($this->userId, $this->appName,
87
            'lastViewedFeedType');
88
89
        // cast from null to int is 0
90
        if($feedType !== null){
91
            $feedType = (int) $feedType;
92
        }
93
94
        // check if feed or folder exists
95
        try {
96
            if($feedType === FeedType::FOLDER){
97
                $this->folderService->find($feedId, $this->userId);
98
99
            } elseif ($feedType === FeedType::FEED){
100
                $this->feedService->find($feedId, $this->userId);
101
102
            // if its the first launch, those values will be null
103
            } elseif($feedType === null){
104
                throw new ServiceNotFoundException('');
105
            }
106
107
        } catch (ServiceNotFoundException $ex){
108
            $feedId = 0;
109
            $feedType = FeedType::SUBSCRIPTIONS;
110
        }
111
112
        return [
113
            'activeFeed' => [
114
                'id' => $feedId,
115
                'type' => $feedType
116
            ]
117
        ];
118
    }
119
120
121
    /**
122
     * @NoAdminRequired
123
     *
124
     * @param string $url
125
     * @param int $parentFolderId
126
     * @param string $title
127
     * @param string $user
128
     * @param string $password
129
     * @return array|\OCP\AppFramework\Http\JSONResponse
130
     */
131
    public function create($url, $parentFolderId, $title=null,
132
                           $user=null, $password=null){
133
        try {
134
            // we need to purge deleted feeds if a feed is created to
135
            // prevent already exists exceptions
136
            $this->feedService->purgeDeleted($this->userId, false);
137
138
            $feed = $this->feedService->create($url, $parentFolderId,
139
                                               $this->userId, $title,
140
                                               $user, $password);
141
            $params = ['feeds' => [$feed]];
142
143
            try {
144
                $params['newestItemId'] =
145
                    $this->itemService->getNewestItemId($this->userId);
146
147
            // An exception occurs if there is a newest item. If there is none,
148
            // simply ignore it and do not add the newestItemId
149
            } catch (ServiceNotFoundException $ex) {}
150
151
            return $params;
152
153
        } catch(ServiceConflictException $ex) {
154
            return $this->error($ex, Http::STATUS_CONFLICT);
155
        } catch(ServiceNotFoundException $ex) {
156
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
157
        }
158
159
    }
160
161
162
    /**
163
     * @NoAdminRequired
164
     *
165
     * @param int $feedId
166
     * @return array|\OCP\AppFramework\Http\JSONResponse
167
     */
168
    public function delete($feedId){
169
        try {
170
            $this->feedService->markDeleted($feedId, $this->userId);
171
        } catch(ServiceNotFoundException $ex) {
172
            return $this->error($ex, Http::STATUS_NOT_FOUND);
173
        }
174
175
        return [];
176
    }
177
178
179
    /**
180
     * @NoAdminRequired
181
     *
182
     * @param int $feedId
183
     * @return array|\OCP\AppFramework\Http\JSONResponse
184
     */
185
    public function update($feedId){
186
        try {
187
            $feed = $this->feedService->update($feedId, $this->userId);
188
189
            return [
190
                'feeds' => [
191
                    // only pass unread count to not accidentally readd
192
                    // the feed again
193
                    [
194
                        'id' => $feed->getId(),
195
                        'unreadCount' => $feed->getUnreadCount()
196
                    ]
197
                ]
198
            ];
199
200
        } catch(ServiceNotFoundException $ex) {
201
            return $this->error($ex, Http::STATUS_NOT_FOUND);
202
        }
203
204
    }
205
206
207
    /**
208
     * @NoAdminRequired
209
     *
210
     * @param array $json
211
     * @return array
212
     */
213
    public function import($json) {
214
        $feed = $this->feedService->importArticles($json, $this->userId);
215
216
        $params = [
217
            'starred' => $this->itemService->starredCount($this->userId)
218
        ];
219
220
        if($feed) {
221
            $params['feeds'] = [$feed];
222
        }
223
224
        return $params;
225
    }
226
227
228
    /**
229
     * @NoAdminRequired
230
     *
231
     * @param int $feedId
232
     * @param int $highestItemId
233
     * @return array
234
     */
235
    public function read($feedId, $highestItemId){
236
        $this->itemService->readFeed($feedId, $highestItemId, $this->userId);
237
238
        return [
239
            'feeds' => [
240
                [
241
                    'id' => $feedId,
242
                    'unreadCount' => 0
243
                ]
244
            ]
245
        ];
246
    }
247
248
249
    /**
250
     * @NoAdminRequired
251
     *
252
     * @param int $feedId
253
     * @return array|\OCP\AppFramework\Http\JSONResponse
254
     */
255
    public function restore($feedId){
256
        try {
257
            $this->feedService->unmarkDeleted($feedId, $this->userId);
258
        } catch(ServiceNotFoundException $ex) {
259
            return $this->error($ex, Http::STATUS_NOT_FOUND);
260
        }
261
262
        return [];
263
    }
264
265
    /**
266
     * @NoAdminRequired
267
     *
268
     * @param int $feedId
269
     * @param bool $pinned
270
     * @param bool $fullTextEnabled
271
     * @param int $updateMode
272
     * @param int $ordering
273
     * @param int $folderId
274
     * @param string $title
275
     */
276
    public function patch($feedId, $pinned=null, $fullTextEnabled=null,
277
                          $updateMode=null, $ordering=null, $title=null,
278
                          $folderId=null) {
279
        $attributes = [
280
            'pinned' => $pinned,
281
            'fullTextEnabled' => $fullTextEnabled,
282
            'updateMode' => $updateMode,
283
            'ordering' => $ordering,
284
            'title' => $title,
285
            'folderId' => $folderId
286
        ];
287
288
        $diff = array_filter($attributes, function ($value) {
289
            return $value !== null;
290
        });
291
292
        try {
293
            $this->feedService->patch($feedId, $this->userId, $diff);
294
        } catch(ServiceNotFoundException $ex) {
295
            return $this->error($ex, Http::STATUS_NOT_FOUND);
296
        }
297
298
        return [];
299
    }
300
301
}
302