FolderController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 149
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A index() 0 4 1
A open() 0 9 2
A create() 0 16 3
A delete() 0 9 2
A rename() 0 16 4
A read() 0 7 1
A restore() 0 9 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\AppFramework\Controller;
18
use \OCP\AppFramework\Http;
19
20
use \OCA\News\Service\FolderService;
21
use \OCA\News\Service\FeedService;
22
use \OCA\News\Service\ItemService;
23
use \OCA\News\Service\ServiceNotFoundException;
24
use \OCA\News\Service\ServiceConflictException;
25
use \OCA\News\Service\ServiceValidationException;
26
27
28
class FolderController extends Controller {
29
30
    use JSONHttpError;
31
32
    private $folderService;
33
    private $feedService;
34
    private $itemService;
35
    private $userId;
36
37
    public function __construct($AppName,
38
                                IRequest $request,
39
                                FolderService $folderService,
40
                                FeedService $feedService,
41
                                ItemService $itemService,
42
                                $UserId) {
43
        parent::__construct($AppName, $request);
44
        $this->folderService = $folderService;
45
        $this->feedService = $feedService;
46
        $this->itemService = $itemService;
47
        $this->userId = $UserId;
48
    }
49
50
51
    /**
52
     * @NoAdminRequired
53
     */
54
    public function index() {
55
        $folders = $this->folderService->findAll($this->userId);
56
        return ['folders' => $folders];
57
    }
58
59
60
    /**
61
     * @NoAdminRequired
62
     *
63
     * @param int $folderId
64
     * @param bool $open
65
     * @return array|\OCP\AppFramework\Http\JSONResponse
66
     */
67
    public function open($folderId, $open) {
68
        try {
69
            $this->folderService->open($folderId, $open, $this->userId);
70
        } catch(ServiceNotFoundException $ex) {
71
            return $this->error($ex, Http::STATUS_NOT_FOUND);
72
        }
73
74
        return [];
75
    }
76
77
78
    /**
79
     * @NoAdminRequired
80
     *
81
     * @param string $folderName
82
     * @return array|\OCP\AppFramework\Http\JSONResponse
83
     */
84
    public function create($folderName) {
85
        try {
86
            // we need to purge deleted folders if a folder is created to
87
            // prevent already exists exceptions
88
            $this->folderService->purgeDeleted($this->userId, false);
89
            $folder = $this->folderService->create($folderName, $this->userId);
90
91
            return ['folders' => [$folder]];
92
93
        } catch(ServiceConflictException $ex) {
94
            return $this->error($ex, Http::STATUS_CONFLICT);
95
        } catch(ServiceValidationException $ex) {
96
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
97
        }
98
99
    }
100
101
102
    /**
103
     * @NoAdminRequired
104
     *
105
     * @param int $folderId
106
     * @return array|\OCP\AppFramework\Http\JSONResponse
107
     */
108
    public function delete($folderId) {
109
        try {
110
            $this->folderService->markDeleted($folderId, $this->userId);
111
        } catch (ServiceNotFoundException $ex){
112
            return $this->error($ex, Http::STATUS_NOT_FOUND);
113
        }
114
115
        return [];
116
    }
117
118
119
    /**
120
     * @NoAdminRequired
121
     *
122
     * @param string $folderName
123
     * @param int $folderId
124
     * @return array|\OCP\AppFramework\Http\JSONResponse
125
     */
126
    public function rename($folderName, $folderId) {
127
        try {
128
            $folder = $this->folderService->rename($folderId, $folderName,
129
                $this->userId);
130
131
            return ['folders' => [$folder]];
132
133
        } catch(ServiceConflictException $ex) {
134
            return $this->error($ex, Http::STATUS_CONFLICT);
135
        } catch(ServiceValidationException $ex) {
136
            return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
137
        } catch (ServiceNotFoundException $ex){
138
            return $this->error($ex, Http::STATUS_NOT_FOUND);
139
        }
140
141
    }
142
143
    /**
144
     * @NoAdminRequired
145
     *
146
     * @param int $folderId
147
     * @param int $highestItemId
148
     * @return array
149
     */
150
    public function read($folderId, $highestItemId) {
151
        $this->itemService->readFolder(
152
            $folderId, $highestItemId, $this->userId
153
        );
154
155
        return ['feeds' => $this->feedService->findAll($this->userId)];
156
    }
157
158
159
    /**
160
     * @NoAdminRequired
161
     *
162
     * @param int $folderId
163
     * @return array|\OCP\AppFramework\Http\JSONResponse
164
     */
165
    public function restore($folderId) {
166
        try {
167
            $this->folderService->unmarkDeleted($folderId, $this->userId);
168
        } catch (ServiceNotFoundException $ex){
169
            return $this->error($ex, Http::STATUS_NOT_FOUND);
170
        }
171
172
        return [];
173
    }
174
175
176
}