Passed
Push — master ( 99686f...b494ba )
by Jean-Christophe
11:21
created

DAOPreparedQueryAll::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 10
cc 4
nc 4
nop 2
crap 4.3731
1
<?php
2
namespace Ubiquity\orm\core\prepared;
3
4
use Ubiquity\events\DAOEvents;
5
use Ubiquity\events\EventsManager;
6
use Ubiquity\orm\OrmUtils;
7
use Ubiquity\orm\DAO;
8
use Ubiquity\cache\database\DbCache;
9
10
/**
11
 * Ubiquity\orm\core\prepared$DAOPreparedQueryAll
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.6
16
 *
17
 */
18
class DAOPreparedQueryAll extends DAOPreparedQuery {
19
20 28
	protected function prepare(?DbCache $cache = null) {
21 28
		$this->conditionParser->setCondition($this->condition);
22 28
		parent::prepare($cache);
23 28
		$this->updatePrepareStatement();
24 28
	}
25
26 1
	public function execute($params = [], $useCache = false) {
27 1
		if ($useCache) {
28
			$rows = $this->db->_optExecuteAndFetch($this->statement, $this->tableName, $this->preparedCondition, $params, $useCache);
29
		} else {
30 1
			$rows = $this->db->_optExecuteAndFetchNoCache($this->statement, $params);
31
		}
32 1
		if ($this->hasIncluded || ! $this->allPublic) {
33 1
			return $this->_parseQueryResponseWithIncluded($rows, $this->className, $useCache);
34
		}
35
		return $this->_parseQueryResponse($rows, $this->className);
36
	}
37
38
	protected function _parseQueryResponse($rows, $className) {
39
		$objects = [];
40
		if ($this->additionalMembers) {
41
			foreach ($rows as $row) {
42
				$object = DAO::_loadSimpleObjectFromRow($this->db, $row, $className, $this->memberList, $this->transformers);
43
				$this->addAditionnalMembers($object, $row);
44
				$objects[OrmUtils::getPropKeyValues($object, $this->propsKeys)] = $object;
45
			}
46
		} else {
47
			foreach ($rows as $row) {
48
				$object = DAO::_loadSimpleObjectFromRow($this->db, $row, $className, $this->memberList, $this->transformers);
49
				$objects[OrmUtils::getPropKeyValues($object, $this->propsKeys)] = $object;
50
			}
51
		}
52
		EventsManager::trigger(DAOEvents::GET_ALL, $objects, $className);
53
		return $objects;
54
	}
55
56 1
	protected function _parseQueryResponseWithIncluded($rows, $className, $useCache) {
57 1
		$objects = [];
58 1
		$invertedJoinColumns = null;
59
60 1
		$oneToManyQueries = $manyToOneQueries = $manyToManyParsers = [];
61 1
		foreach ($rows as $row) {
62 1
			$object = DAO::_loadObjectFromRow($this->db, $row, $className, $invertedJoinColumns, $manyToOneQueries, $this->oneToManyFields, $this->manyToManyFields, $oneToManyQueries, $manyToManyParsers, $this->memberList, $this->accessors, $this->transformers);
63 1
			$key = OrmUtils::getPropKeyValues($object, $this->propsKeys);
64 1
			if ($this->additionalMembers) {
65
				$this->addAditionnalMembers($object, $row);
66
			}
67 1
			$objects[$key] = $object;
68
		}
69 1
		DAO::_affectsRelationObjects($className, $this->firstPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $this->included, $useCache);
70 1
		EventsManager::trigger(DAOEvents::GET_ALL, $objects, $className);
71 1
		return $objects;
72
	}
73
}
74