Completed
Push — master ( 060b04...657701 )
by
unknown
04:35
created

FolderMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 3
rs 10
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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;
15
16
use OCP\IDBConnection;
17
use OCP\AppFramework\Db\Entity;
18
19
class FolderMapper extends NewsMapper {
20
21 16
    public function __construct(IDBConnection $db) {
22 16
        parent::__construct($db, 'news_folders', Folder::class);
23 16
    }
24
25
    public function find($id, $userId){
26
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
27
            'WHERE `id` = ? ' .
28
            'AND `user_id` = ?';
29
30
        return $this->findEntity($sql, [$id, $userId]);
31
    }
32
33
34
    public function findAllFromUser($userId){
35
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
36
            'WHERE `user_id` = ? ' .
37
            'AND `deleted_at` = 0';
38
        $params = [$userId];
39
40
        return $this->findEntities($sql, $params);
41
    }
42
43
44
    public function findByName($folderName, $userId){
45
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
46
            'WHERE `name` = ? ' .
47
            'AND `user_id` = ?';
48
        $params = [$folderName, $userId];
49
50
        return $this->findEntities($sql, $params);
51
    }
52
53
54
    public function delete(Entity $entity){
55
        parent::delete($entity);
56
57
        // someone please slap me for doing this manually :P
58
        // we needz CASCADE + FKs please
59
        $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?';
60
        $params = [$entity->getId()];
61
        $stmt = $this->execute($sql, $params);
62
        $stmt->closeCursor();
63
64
        $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '.
65
            '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)';
66
67
        $stmt = $this->execute($sql);
68
        $stmt->closeCursor();
69
    }
70
71
72
    /**
73
     * @param int $deleteOlderThan if given gets all entries with a delete date
74
     * older than that timestamp
75
     * @param string $userId if given returns only entries from the given user
76
     * @return array with the database rows
77
     */
78
    public function getToDelete($deleteOlderThan=null, $userId=null) {
79
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
80
            'WHERE `deleted_at` > 0 ';
81
        $params = [];
82
83
        // sometimes we want to delete all entries
84
        if ($deleteOlderThan !== null) {
85
            $sql .= 'AND `deleted_at` < ? ';
86
            $params[] = $deleteOlderThan;
87
        }
88
89
        // we need to sometimes only delete feeds of a user
90
        if($userId !== null) {
91
            $sql .= 'AND `user_id` = ?';
92
            $params[] = $userId;
93
        }
94
95
        return $this->findEntities($sql, $params);
96
    }
97
98
99
    /**
100
     * Deletes all folders of a user
101
     * @param string $userId the name of the user
102
     */
103
    public function deleteUser($userId) {
104
        $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?';
105
        $this->execute($sql, [$userId]);
106
    }
107
108
109
}
110