Passed
Push — master ( 06f7cb...c3c92a )
by Fabio
05:08
created

TDbPluginModule::getSqliteDatabaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * TDbPluginModule class file
5
 *
6
 * @author Brad Anderson <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado
10
 */
11
	
12
namespace Prado\Util;
13
14
use Prado\Data\TDataSourceConfig;
15
use Prado\Data\TDbConnection;
16
use Prado\Exceptions\TConfigurationException;
17
18
/**
19
 * TDbPluginModule class.
20
 *
21
 * TDbPluginModule adds database connectivity to the plugin modules. This standardizes
22
 * the Database Connectivity for Plugins. Also TParameterizeBehavior can be used to set
23
 * all TDbPluginModule::ConnectionID with one setting.
24
 *
25
 * @author Brad Anderson <[email protected]>
26
 * @package Prado
27
 * @since 4.2.0
28
 */
29
30
class TDbPluginModule extends TPluginModule implements \Prado\Util\IDbModule
31
{
32
	/**
33
	 * @var string the ID of TDataSourceConfig module
34
	 */
35
	private $_connID = '';
36
	/**
37
	 * @var TDbConnection the DB connection instance
38
	 */
39
	private $_conn;
40
41
	/**
42
	 * @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set.
43
	 */
44
	public function getConnectionID()
45
	{
46
		return $this->_connID;
47
	}
48
49
	/**
50
	 * Sets the ID of a TDataSourceConfig module.
51
	 * The datasource module will be used to establish the DB connection
52
	 * that will be used by the plugin.
53
	 * @param string $value module ID.
54
	 */
55
	public function setConnectionID($value)
56
	{
57
		$this->_connID = $value;
58
	}
59
60
	/**
61
	 * @return TDbConnection the database connection that may be used to retrieve user data.
62
	 */
63
	public function getDbConnection()
64
	{
65
		if ($this->_conn === null) {
66
			$this->_conn = $this->createDbConnection($this->_connID);
67
			$this->_conn->setActive(true);
68
		}
69
		return $this->_conn;
70
	}
71
72
	/**
73
	 * Creates the DB connection.  If no ConnectionId is available, this
74
	 * will try to start a sqlite database if the subclass has a name via
75
	 * {@link getSqliteDatabaseName}.
76
	 * @param string $connectionID the module ID for TDataSourceConfig
77
	 * @throws TConfigurationException if module ID is invalid or empty
78
	 * without a Sqlite database.
79
	 * @return TDbConnection the created DB connection
80
	 */
81
	protected function createDbConnection($connectionID)
82
	{
83
		if ($connectionID !== '') {
84
			$conn = $this->getApplication()->getModule($connectionID);
85
			if ($conn instanceof TDataSourceConfig) {
86
				return $conn->getDbConnection();
87
			} else {
88
				throw new TConfigurationException('dbpluginmodule_connectionid_invalid', $connectionID);
89
			}
90
		} else {
91
			if ($file = $this->getSqliteDatabaseName()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $file is correct as $this->getSqliteDatabaseName() targeting Prado\Util\TDbPluginModu...getSqliteDatabaseName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
				$db = new TDbConnection;
93
				// default to SQLite3 database
94
				$dbFile = $this->getApplication()->getRuntimePath() . DIRECTORY_SEPARATOR . $file;
0 ignored issues
show
Bug introduced by
Are you sure $file of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
				$dbFile = $this->getApplication()->getRuntimePath() . DIRECTORY_SEPARATOR . /** @scrutinizer ignore-type */ $file;
Loading history...
95
				$db->setConnectionString('sqlite:' . $dbFile);
96
				return $db;
97
			} else {
98
				throw new TConfigurationException('dbpluginmodule_connectionid_required');
99
			}
100
		}
101
	}
102
	
103
	/**
104
	 * @return null|string if the sub-class wants a sqlite db then return the name.
105
	 */
106
	protected function getSqliteDatabaseName()
107
	{
108
		return null;
109
	}
110
}
111