DbConnectionTrait::setConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Trait for handling connections pool functionality
4
 *
5
 * @file      DbConnectionTrait.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      2013-12-31 15:44
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\DataBase\Adapters;
17
18
use Veles\DataBase\ConnectionPools\ConnectionPool;
19
20
/**
21
 * Trait DbConnectionTrait
22
 *
23
 * Contains connection pool getters and setters
24
 *
25
 * @author  Alexander Yancharuk <alex at itvault dot info>
26
 */
27
trait DbConnectionTrait
28
{
29
	/** @var ConnectionPool */
30
	protected $pool;
31
	/** @var  \PDO */
32
	protected $resource;
33
	/** @var  string */
34
	protected $connection_name;
35
36
	/**
37
	 * Add connection pool
38
	 *
39
	 * @param ConnectionPool $pool
40
	 *
41
	 * @return $this
42
	 */
43 28
	public function setPool(ConnectionPool $pool)
44
	{
45 28
		$this->pool            = $pool;
46 28
		$this->connection_name = $pool->getDefaultConnectionName();
47
48 28
		return $this;
49
	}
50
51
	/**
52
	 * Get connection pool
53
	 *
54
	 * @return ConnectionPool $pool
55
	 */
56 1
	public function getPool()
57
	{
58 1
		return $this->pool;
59
	}
60
61
	/**
62
	 * Set default connection
63
	 *
64
	 * @param string $name Connection name
65
	 *
66
	 * @return $this
67
	 */
68 1
	public function setConnection($name)
69
	{
70 1
		$this->connection_name = $name;
71 1
		$this->resource        = null;
72
73 1
		return $this;
74
	}
75
76
	/**
77
	 * Get default connection resource
78
	 *
79
	 * return \PDO
80
	 */
81 27
	public function getResource()
82
	{
83 27
		if (null === $this->resource) {
84 27
			$this->resource = $this->pool
85 27
				->getConnection($this->connection_name)
86 27
				->getResource();
87
		}
88
89 27
		return $this->resource;
90
	}
91
}
92