Test Failed
Pull Request — master (#197)
by
unknown
10:13
created

AbstractRepository::byId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Ubiquity\orm\repositories;
4
5
use Ubiquity\controllers\Controller;
6
use Ubiquity\orm\DAO;
7
use Ubiquity\views\View;
8
9
/**
10
 * A repository for managing CRUD operations on a model.
11
 * Ubiquity\orm\repositories$AbstractRepository
12
 * This class is part of Ubiquity
13
 *
14
 * @author jc
15
 * @version 1.0.2
16
 *
17
 */
18
abstract class AbstractRepository {
19
20
	abstract protected function getModel(): string;
21
22
	/**
23
	 * Load all instances.
24
	 *
25
	 * @param string $condition
26
	 * @param array|boolean $included
27
	 * @param array $parameters
28
	 * @param bool $useCache
29
	 * @return array
30
	 */
31
	public function all(string $condition = '', $included = false, array $parameters = [ ], bool $useCache = false): array {
32
		return DAO::getAll ( $this->getModel (), $condition, $included, $parameters, $useCache );
33
	}
34
35
	/**
36
	 * Load one instance by id.
37
	 *
38
	 * @param $keyValues
39
	 * @param bool|array $included
40
	 * @param bool $useCache
41
	 * @return ?object
42
	 */
43
	public function byId($keyValues, $included = true, bool $useCache = false): ?object {
44
		return DAO::getById ( $this->getModel (), $keyValues, $included, $useCache );
45
	}
46
47
	/**
48
	 * Load one instance.
49
	 *
50
	 * @param string $condition
51
	 * @param bool|array $included
52
	 * @param array $parameters
53
	 * @param bool $useCache
54
	 * @return ?object
55
	 * @throws \Ubiquity\exceptions\DAOException
56
	 */
57
	public function one(string $condition = '', $included = true, array $parameters = [ ], bool $useCache = false): ?object {
58
		return DAO::getOne ( $this->getModel (), $condition, $included, $parameters, $useCache );
59
	}
60
61
	/**
62
	 * Insert a new instance $instance into the database.
63
	 *
64
	 * @param object $instance
65
	 * @param bool $insertMany
66
	 * @return bool
67
	 * @throws \Exception
68
	 */
69
	public function insert(object $instance, $insertMany = false): bool {
70
		return DAO::insert ( $instance, $insertMany );
71
	}
72
73
	/**
74
	 * Update an instance $instance in the database.
75
	 *
76
	 * @param object $instance
77
	 * @param bool $insertMany
78
	 * @return bool
79
	 */
80
	public function update(object $instance, $insertMany = false): bool {
81
		return DAO::update ( $instance, $insertMany );
82
	}
83
84
	/**
85
	 * Save (insert or update) an instance $instance in the database.
86
	 *
87
	 * @param object $instance
88
	 * @param bool $insertMany
89
	 * @return bool|int
90
	 */
91
	public function save(object $instance, $insertMany = false) {
92
		return DAO::save ( $instance, $insertMany );
93
	}
94
95
	/**
96
	 * Remove an instance $instance from the database.
97
	 *
98
	 * @param object $instance
99
	 * @return int|null
100
	 */
101
	public function remove(object $instance): ?int {
102
		return DAO::remove ( $instance );
103
	}
104
	
105
	/**
106
	 * Returns the number of instances.
107
	 * 
108
	 * @param string $condition
109
	 * @param array $parameters
110
	 * @return int
111
	 */
112
	public function count(string $condition='',?array $parameters=null):int {
113
		return DAO::count($this->getModel(),$condition,$parameters);
1 ignored issue
show
Bug Best Practice introduced by
The expression return Ubiquity\orm\DAO:...condition, $parameters) could return the type false which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
114
	}
115
}
116