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

Accessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 53
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 1
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A db() 0 14 3
A db_prime() 0 11 2
cdb() 0 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