Passed
Pull Request — master (#786)
by
unknown
06:41
created

TDbPluginModule   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 77
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setConnectionID() 0 3 1
A getDbConnection() 0 7 2
A createDbConnection() 0 18 4
A getConnectionID() 0 3 1
A getSqliteDatabaseName() 0 3 1
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\Util\XTPluginModule;
0 ignored issues
show
Bug introduced by
The type Prado\Util\XTPluginModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Prado\Data\TDataSourceConfig;
16
use Prado\Data\TDbConnection;
17
use Prado\Exceptions\TConfigurationException;
18
19
/**
20
 * TDbPluginModule class.
21
 *
22
 * TDbPluginModule adds database connectivity to the plugin modules. This standardizes
23
 * the Database Connectivity for Plugins. Also TParameterizeBehavior can be used to set
24
 * all TDbPluginModule::ConnectionID with one setting.
25
 *
26
 * @author Brad Anderson <[email protected]>
27
 * @package Prado
28
 * @since 4.2.0
29
 */
30
31
class TDbPluginModule extends TPluginModule implements \Prado\Util\IDbModule
32
{
33
	/**
34
	 * @var string the ID of TDataSourceConfig module
35
	 */
36
	private $_connID = '';
37
	/**
38
	 * @var TDbConnection the DB connection instance
39
	 */
40
	private $_conn;
41
42
	/**
43
	 * @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set.
44
	 */
45
	public function getConnectionID()
46
	{
47
		return $this->_connID;
48
	}
49
50
	/**
51
	 * Sets the ID of a TDataSourceConfig module.
52
	 * The datasource module will be used to establish the DB connection
53
	 * that will be used by the plugin.
54
	 * @param string $value module ID.
55
	 */
56
	public function setConnectionID($value)
57
	{
58
		$this->_connID = $value;
59
	}
60
61
	/**
62
	 * @return TDbConnection the database connection that may be used to retrieve user data.
63
	 */
64
	public function getDbConnection()
65
	{
66
		if ($this->_conn === null) {
67
			$this->_conn = $this->createDbConnection($this->_connID);
68
			$this->_conn->setActive(true);
69
		}
70
		return $this->_conn;
71
	}
72
73
	/**
74
	 * Creates the DB connection.  Override this method to create a sqlite
75
	 * database when no connection ID is available.
76
	 * @param string $connectionID the module ID for TDataSourceConfig
77
	 * @throws TConfigurationException if module ID is invalid or empty
78
	 * @return TDbConnection the created DB connection
79
	 */
80
	protected function createDbConnection($connectionID)
81
	{
82
		if ($connectionID !== '') {
83
			$conn = $this->getApplication()->getModule($connectionID);
84
			if ($conn instanceof TDataSourceConfig) {
85
				return $conn->getDbConnection();
86
			} else {
87
				throw new TConfigurationException('dbpluginmodule_connectionid_invalid', $connectionID);
88
			}
89
		} else {
90
			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...
91
				$db = new TDbConnection;
92
				// default to SQLite3 database
93
				$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

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