Completed
Push — master ( 5c0dbc...42c9a9 )
by Nazar
04:27
created

Accessor::db_prime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2013-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\DB;
9
use
10
	cs\DB;
11
12
/**
13
 * Accessor trait
14
 *
15
 * Provides db() and db_prime() methods for simplified for with DB
16
 */
17
trait Accessor {
18
	/**
19
	 * Link to db object
20
	 * @var false|_Abstract
21
	 */
22
	private $_db = false;
23
	/**
24
	 * Link to primary db object
25
	 * @var false|_Abstract
26
	 */
27
	private $_db_prime = false;
28
	/**
29
	 * Returns link to the object of db for reading (can be mirror of main DB)
30
	 *
31
	 * @return _Abstract
32
	 */
33 36
	function db () {
34 36
		if (is_object($this->_db)) {
35 36
			return $this->_db;
36
		}
37 36
		if (is_object($this->_db_prime)) {
38 22
			return $this->_db = $this->_db_prime;
39
		}
40
		/**
41
		 * Save reference for faster access
42
		 */
43
		/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
44 30
		$this->_db = DB::instance()->db($this->cdb());
45 30
		return $this->_db;
46
	}
47
	/**
48
	 * Returns link to the object of db for writing (always main DB)
49
	 *
50
	 * @return _Abstract
51
	 */
52 34
	function db_prime () {
53 34
		if (is_object($this->_db_prime)) {
54 34
			return $this->_db_prime;
55
		}
56
		/**
57
		 * Save reference for faster access
58
		 */
59
		/** @noinspection ExceptionsAnnotatingAndHandlingInspection */
60 34
		$this->_db_prime = DB::instance()->db_prime($this->cdb());
61 34
		return $this->_db_prime;
62
	}
63
	/**
64
	 * Returns database index
65
	 *
66
	 * @return int
67
	 */
68
	abstract protected function cdb ();
69
}
70