Completed
Push — master ( 4f8b81...65d286 )
by
unknown
05:00
created

ItemMapper::readItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 4
crap 6
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\Db\Mysql;
15
16
use OCP\IDBConnection;
17
18
use OCA\News\Db\StatusFlag;
19
20
21
class ItemMapper extends \OCA\News\Db\ItemMapper {
22
23 3
    public function __construct(IDBConnection $db){
24 3
        parent::__construct($db);
25 3
    }
26
27
28
    /**
29
     * Delete all items for feeds that have over $threshold unread and not
30
     * starred items
31
	 * @param int $threshold the number of items that should be deleted
32
     */
33 2
    public function deleteReadOlderThanThreshold($threshold){
34 2
        $status = StatusFlag::STARRED | StatusFlag::UNREAD;
35
        $sql = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`, ' .
36 2
        '`feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' .
37 2
            'FROM `*PREFIX*news_items` `items` ' .
38 2
            'JOIN `*PREFIX*news_feeds` `feeds` ' .
39 2
                'ON `feeds`.`id` = `items`.`feed_id` ' .
40 2
                'AND NOT ((`items`.`status` & ?) > 0) ' .
41 2
            'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' .
42 2
            'HAVING COUNT(*) > ?';
43 2
        $params = [$status, $threshold];
44 2
        $result = $this->execute($sql, $params);
45
46 2
        while($row = $result->fetch()) {
47
48 2
            $size = (int) $row['size'];
49 2
            $limit = $size - $threshold;
50
51 2
            if($limit > 0) {
52 1
                $params = [$status, $row['feed_id'], $limit];
53
54
                $sql = 'DELETE FROM `*PREFIX*news_items` ' .
55 1
                    'WHERE NOT ((`status` & ?) > 0) ' .
56 1
                    'AND `feed_id` = ? ' .
57 1
                    'ORDER BY `id` ASC ' .
58 1
                    'LIMIT ?';
59
60 1
                $this->execute($sql, $params);
61 1
            }
62 2
        }
63
64 2
    }
65
66
    public function readItem($itemId, $isRead, $lastModified, $userId) {
67
        if ($isRead) {
68
            $sql = 'UPDATE `*PREFIX*news_items` `items`
69
                JOIN `*PREFIX*news_feeds` `feeds`
70
                    ON `feeds`.`id` = `items`.`feed_id`
71
                SET `items`.`status` = `items`.`status` & ?
72
                    AND `items`.`last_modified` = ?
73
                WHERE `items`.`fingerprint` = ?
74
                    AND `feeds`.`user_id` = ?';
75
            $params = [~StatusFlag::UNREAD, $lastModified,
76
                       $item->getFingerprint(), $userId];
0 ignored issues
show
Bug introduced by
The variable $item does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
77
            $this->execute($sql, $params);
78
        } else {
79
            // no other behavior for mysql if should be marked unread
80
            parent::readItem($itemId, $isRead, $lastModified, $userId);
81
        }
82
    }
83
84
}
85