Test Failed
Push — master ( b46f28...e0ce72 )
by Jean-Christophe
12:10
created

DAOPooling::pool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * Ubiquity\orm\traits$DAOPooling
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 *
14
 * @property array $db
15
 *
16
 */
17
trait DAOPooling {
18
19
	abstract public static function startDatabase(&$config, $offset = null);
20
	protected static $pool;
21
22
	/**
23
	 * gets a new DbConnection from pool
24
	 *
25
	 * @param string $offset
26
	 * @return mixed
27
	 */
28
	public static function pool($offset = 'default') {
29
		if (! isset ( self::$db [$offset] )) {
30
			self::startDatabase ( Startup::$config, $offset );
31
		}
32
		return self::$db [$offset]->pool ();
33
	}
34
35
	public static function freePool($db) {
36
		self::$pool->put ( $db );
37
	}
38
39
	public static function go($asyncCallable, $offset = 'default') {
40
		$vars = \get_defined_vars ();
41
		\Swoole\Coroutine::create ( function () use ($vars, $asyncCallable, $offset) {
42
			$db = self::pool ( $offset );
43
			\call_user_func_array ( $asyncCallable, $vars );
44
			self::freePool ( $db );
45
		} );
46
	}
47
}
48
49