Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.62% covered (danger)
47.62%
10 / 21
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BillMapper
47.62% covered (danger)
47.62%
10 / 21
33.33% covered (danger)
33.33%
1 / 3
8.59
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 find
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
2.00
 findProjectId
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Nextcloud - cospend
5 *
6 * This file is licensed under the Affero General Public License version 3 or
7 * later. See the COPYING file.
8 *
9 * @author Julien Veyssier <eneiluj@posteo.net
10 * @copyright Julien Veyssier 2019
11 */
12
13 namespace OCA\Cospend\Db;
14
15use Exception;
16use OCP\AppFramework\Db\QBMapper;
17use OCP\DB\QueryBuilder\IQueryBuilder;
18use OCP\IDBConnection;
19
20class BillMapper extends QBMapper {
21    const TABLENAME = 'cospend_bills';
22
23    public function __construct(IDBConnection $db) {
24        parent::__construct($db, self::TABLENAME, Bill::class);
25    }
26
27    /**
28     * @param int $id
29     * @return Bill
30     * @throws \OCP\DB\Exception
31     */
32    public function find(int $id): Bill {
33        $qb = $this->db->getQueryBuilder();
34        $qb->select('*')
35            ->from(self::TABLENAME)
36            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
37        $result = $qb->executeQuery();
38        $row = $result->fetch();
39        $result->closeCursor();
40
41        if ($row === false) {
42            throw new Exception('Bill ' . $id . ' not found');
43        }
44
45        return $this->mapRowToEntity($row);
46    }
47
48    public function findProjectId(int $id): string {
49        $qb = $this->db->getQueryBuilder();
50        $qb->select('projectid')
51            ->from(self::TABLENAME)
52            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
53        $result = $qb->executeQuery();
54        $row = $result->fetch();
55        $result->closeCursor();
56
57        if ($row === false) {
58            throw new Exception('Bill ' . $id . ' not found');
59        }
60
61        return $row['projectid'];
62    }
63}