DaoCommon::getRecord()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace AOE\Languagevisibility\Dao;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
/**
30
 * Class DaoCommon
31
 * @package AOE\Languagevisibility
32
 */
33
class DaoCommon {
34
35
	/**
36
	 * @var \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
37
	 */
38
	protected static $cache = NULL;
39
40
	/**
41
	 * Gets a record by UID and table
42
	 *
43
	 * @param int $uid
44
	 * @param string $table
45
	 * @return array
46
	 */
47
	public static function getRecord($uid, $table) {
48
		$cacheKey = sha1($table . $uid);
49
50
		if (!self::getCache()->has($cacheKey)) {
51
			self::getCache()->set($cacheKey, self::getRequestedRecord($uid, $table));
52
		}
53
54
		return self::getCache()->get($cacheKey);
55
	}
56
57
	/**
58
	 * Gets a requested record
59
	 *
60
	 * @param int $uid
61
	 * @param string $table
62
	 * @return array
63
	 */
64
	protected static function getRequestedRecord($uid, $table) {
65
		$result = self::getDatabase()->exec_SELECTquery(
66
			'*',
67
			$table,
68
			'uid=' . intval($uid)
69
		);
70
		$row = self::getDatabase()->sql_fetch_assoc($result);
71
		self::getDatabase()->sql_free_result($result);
72
73
		return $row;
74
	}
75
76
	/**
77
	 * Gets records by table and where clause
78
	 *
79
	 * @param string $table
80
	 * @param string $where
81
	 * @return array
82
	 */
83
	public static function getRecords($table, $where) {
84
		$cacheKey = sha1($table . $where);
85
86
		if (!self::getCache()->has($cacheKey)) {
87
			self::getCache()->set($cacheKey, self::getRequestedRecords($table, $where));
88
		}
89
90
		return self::getCache()->get($cacheKey);
91
	}
92
93
	/**
94
	 * Gets requested records by table and where clause
95
	 *
96
	 * @param string $table
97
	 * @param string $where
98
	 * @return array
99
	 */
100
	protected static function getRequestedRecords($table, $where) {
101
		$result = self::getDatabase()->exec_SELECTquery(
102
			'*',
103
			$table,
104
			$where
105
		);
106
		$rows = array();
107
		while ($row = self::getDatabase()->sql_fetch_assoc($result)) {
108
			$rows[] = $row;
109
		}
110
		self::getDatabase()->sql_free_result($result);
111
112
		return $rows;
113
	}
114
115
	/**
116
	 * Gets the cache
117
	 *
118
	 * @return \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
119
	 */
120
	protected static function getCache() {
121
		if (!self::$cache) {
122
			self::$cache = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')
123
				->getCache('tx_languagevisibility');
124
		}
125
126
		return self::$cache;
127
	}
128
129
	/**
130
	 * Gets a database connection
131
	 *
132
	 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
133
	 */
134
	protected function getDatabase() {
135
		return $GLOBALS['TYPO3_DB'];
136
	}
137
}
138