Completed
Push — integration-test-cleanup ( 7645d1 )
by
unknown
05:02
created

NewsMapper::where()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

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 19
ccs 0
cts 12
cp 0
rs 9.4286
cc 2
eloc 11
nc 2
nop 1
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;
15
16
use OCP\IDBConnection;
17
use OCP\AppFramework\Db\Mapper;
18
19
abstract class NewsMapper extends Mapper {
20
21 58
    public function __construct(IDBConnection $db, $table, $entity) {
22 58
        parent::__construct($db, $table, $entity);
23 58
    }
24
25
    /**
26
     * @param int $id the id of the feed
27
     * @param string $userId the id of the user
28
     * @return \OCP\AppFramework\Db\Entity
29
     */
30
    abstract public function find($id, $userId);
31
32
    /**
33
     * Performs a SELECT query with all arguments appened to the WHERE clause
34
     * The SELECT will be performed on the current table and take the entity
35
     * that is related for transforming the properties into column names
36
     * @param array $search an assoc array from property to filter value
37
     * @return array
38
     */
39
    public function where(array $search) {
40
        $entity = new $this->entityClass;
41
42
        // turn keys into sql query filter, e.g. feedId -> feed_id = :feedId
43
        $filter = array_map(function ($property) use ($entity) {
44
            $column = $entity->propertyToColumn($property);
45
            return $column . ' = :' . $property;
46
        }, array_keys($search));
47
48
        $andStatement = implode(' AND ', $filter);
49
50
        $sql = 'SELECT * FROM `' . $this->getTableName() . '`';
51
52
        if (count($search) > 0) {
53
            $sql .= 'WHERE ' . $andStatement;
54
        }
55
56
        return $this->findEntities($sql, $search);
57
    }
58
59
}