ItemService::findAll()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Service;
15
16
use OCP\IConfig;
17
use OCP\AppFramework\Db\DoesNotExistException;
18
use OCP\AppFramework\Utility\ITimeFactory;
19
20
use OCA\News\Db\ItemMapper;
21
use OCA\News\Db\StatusFlag;
22
use OCA\News\Db\FeedType;
23
use OCA\News\Config\Config;
24
25
26
class ItemService extends Service {
27
28
    private $statusFlag;
29
    private $config;
30
    private $timeFactory;
31
    private $itemMapper;
32
    private $systemConfig;
33
34
    public function __construct(ItemMapper $itemMapper,
35
                                StatusFlag $statusFlag,
36
                                ITimeFactory $timeFactory,
37
                                Config $config,
38
                                IConfig $systemConfig){
39
        parent::__construct($itemMapper);
40
        $this->statusFlag = $statusFlag;
41
        $this->config = $config;
42
        $this->timeFactory = $timeFactory;
43
        $this->itemMapper = $itemMapper;
44
        $this->systemConfig = $systemConfig;
45
    }
46
47
48
    /**
49
     * Returns all new items
50
     * @param int $id the id of the feed, 0 for starred or all items
51
     * @param int $type the type of the feed
52
     * @param int $updatedSince a timestamp with the last modification date
53
     * returns only items with a >= modified timestamp
54
     * @param boolean $showAll if unread items should also be returned
55
     * @param string $userId the name of the user
56
     * @return array of items
57
     */
58
    public function findAllNew($id, $type, $updatedSince, $showAll, $userId){
59
        $status = $this->statusFlag->typeToStatus($type, $showAll);
60
61
        switch($type){
62
            case FeedType::FEED:
63
                return $this->itemMapper->findAllNewFeed(
64
                    $id, $updatedSince, $status, $userId
65
                );
66
            case FeedType::FOLDER:
67
                return $this->itemMapper->findAllNewFolder(
68
                    $id, $updatedSince, $status, $userId
69
                );
70
            default:
71
                return $this->itemMapper->findAllNew(
72
                    $updatedSince, $status, $userId
73
                );
74
        }
75
    }
76
77
78
    /**
79
     * Returns all items
80
     * @param int $id the id of the feed, 0 for starred or all items
81
     * @param int $type the type of the feed
82
     * @param int $limit how many items should be returned
83
     * @param int $offset the offset
84
     * @param boolean $showAll if unread items should also be returned
85
     * @param boolean $oldestFirst if it should be ordered by oldest first
86
     * @param string $userId the name of the user
87
     * @param string[] $search an array of keywords that the result should
88
     * contain in either the author, title, link or body
89
     * @return array of items
90
     */
91
    public function findAll($id, $type, $limit, $offset, $showAll, $oldestFirst,
92
                            $userId, $search=[]){
93
        $status = $this->statusFlag->typeToStatus($type, $showAll);
94
95
        switch($type){
96
            case FeedType::FEED:
97
                return $this->itemMapper->findAllFeed(
98
                    $id, $limit, $offset, $status, $oldestFirst, $userId,
99
                    $search
100
                );
101
            case FeedType::FOLDER:
102
                return $this->itemMapper->findAllFolder(
103
                    $id, $limit, $offset, $status, $oldestFirst, $userId,
104
                    $search
105
                );
106
            default:
107
                return $this->itemMapper->findAll(
108
                    $limit, $offset, $status, $oldestFirst, $userId, $search
109
                );
110
        }
111
    }
112
113
114
    /**
115
     * Star or unstar an item
116
     * @param int $feedId the id of the item's feed that should be starred
117
     * @param string $guidHash the guidHash of the item that should be starred
118
     * @param boolean $isStarred if true the item will be marked as starred,
119
     * if false unstar
120
     * @param string $userId the name of the user for security reasons
121
     * @throws ServiceNotFoundException if the item does not exist
122
     */
123
    public function star($feedId, $guidHash, $isStarred, $userId){
124
        try {
125
            $item = $this->itemMapper->findByGuidHash(
126
                $guidHash, $feedId, $userId
127
            );
128
129
            $item->setLastModified($this->timeFactory->getTime());
130
            if($isStarred){
131
                $item->setStarred();
132
            } else {
133
                $item->setUnstarred();
134
            }
135
            $this->itemMapper->update($item);
136
        } catch(DoesNotExistException $ex) {
137
            throw new ServiceNotFoundException($ex->getMessage());
138
        }
139
    }
140
141
142
    /**
143
     * Read or unread an item
144
     * @param int $itemId the id of the item that should be read
145
     * @param boolean $isRead if true the item will be marked as read,
146
     * if false unread
147
     * @param string $userId the name of the user for security reasons
148
     * @throws ServiceNotFoundException if the item does not exist
149
     */
150
    public function read($itemId, $isRead, $userId){
151
        $lastModified = $this->timeFactory->getTime();
152
        $this->itemMapper->readItem($itemId, $isRead, $lastModified, $userId);
153
    }
154
155
156
    /**
157
     * Set all items read
158
     * @param int $highestItemId all items below that are marked read. This is
159
     * used to prevent marking items as read that the users hasn't seen yet
160
     * @param string $userId the name of the user
161
     */
162
    public function readAll($highestItemId, $userId){
163
        $time = $this->timeFactory->getTime();
164
        $this->itemMapper->readAll($highestItemId, $time, $userId);
165
    }
166
167
168
    /**
169
     * Set a folder read
170
     * @param int $folderId the id of the folder that should be marked read
171
     * @param int $highestItemId all items below that are marked read. This is
172
     * used to prevent marking items as read that the users hasn't seen yet
173
     * @param string $userId the name of the user
174
     */
175
    public function readFolder($folderId, $highestItemId, $userId){
176
        $time = $this->timeFactory->getTime();
177
        $this->itemMapper->readFolder(
178
            $folderId, $highestItemId, $time, $userId
179
        );
180
    }
181
182
183
    /**
184
     * Set a feed read
185
     * @param int $feedId the id of the feed that should be marked read
186
     * @param int $highestItemId all items below that are marked read. This is
187
     * used to prevent marking items as read that the users hasn't seen yet
188
     * @param string $userId the name of the user
189
     */
190
    public function readFeed($feedId, $highestItemId, $userId){
191
        $time = $this->timeFactory->getTime();
192
        $this->itemMapper->readFeed($feedId, $highestItemId, $time, $userId);
193
    }
194
195
196
    /**
197
     * This method deletes all unread feeds that are not starred and over the
198
     * count of $this->autoPurgeCount starting by the oldest. This is to clean
199
     * up the database so that old entries don't spam your db. As criteria for
200
     * old, the id is taken
201
     */
202
    public function autoPurgeOld(){
203
        $count = $this->config->getAutoPurgeCount();
204
        if ($count >= 0) {
205
            $this->itemMapper->deleteReadOlderThanThreshold($count);
206
        }
207
    }
208
209
210
    /**
211
     * Returns the newest item id, use this for marking feeds read
212
     * @param string $userId the name of the user
213
     * @throws ServiceNotFoundException if there is no newest item
214
     * @return int
215
     */
216
    public function getNewestItemId($userId) {
217
        try {
218
            return $this->itemMapper->getNewestItemId($userId);
219
        } catch(DoesNotExistException $ex) {
220
            throw new ServiceNotFoundException($ex->getMessage());
221
        }
222
    }
223
224
225
    /**
226
     * Returns the starred count
227
     * @param string $userId the name of the user
228
     * @return int the count
229
     */
230
    public function starredCount($userId){
231
        return $this->itemMapper->starredCount($userId);
232
    }
233
234
235
    /**
236
     * @param string $userId from which user the items should be taken
237
     * @return array of items which are starred or unread
238
     */
239
    public function getUnreadOrStarred($userId) {
240
        return $this->itemMapper->findAllUnreadOrStarred($userId);
241
    }
242
243
244
    /**
245
     * Deletes all items of a user
246
     * @param string $userId the name of the user
247
     */
248
    public function deleteUser($userId) {
249
        $this->itemMapper->deleteUser($userId);
250
    }
251
252
253
    /**
254
     * Regenerates the search index for all items
255
     */
256
    public function generateSearchIndices() {
257
        $this->itemMapper->updateSearchIndices();
258
    }
259
260
}
261