Database::getConnection()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Database;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
8
// |                                                                           |
9
// | For the full copyright and license information, please view the LICENSE   |
10
// | file that was distributed with this source code. You can also view the    |
11
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
12
// |   vi: set noexpandtab:                                                    |
13
// |   Local Variables:                                                        |
14
// |   indent-tabs-mode: t                                                     |
15
// |   End:                                                                    |
16
// +---------------------------------------------------------------------------+
17
18
use Agavi\Exception\DatabaseException;
19
use Agavi\Exception\InitializationException;
20
use Agavi\Util\ParameterHolder;
21
22
/**
23
 * AgaviDatabase is a base abstraction class that allows you to setup any type
24
 * of database connection via a configuration file.
25
 *
26
 * @package    agavi
27
 * @subpackage database
28
 *
29
 * @author     Sean Kerr <[email protected]>
30
 * @author     David Zülke <[email protected]>
31
 * @copyright  Authors
32
 * @copyright  The Agavi Project
33
 *
34
 * @since      0.9.0
35
 *
36
 * @version    $Id$
37
 */
38
abstract class Database extends ParameterHolder
39
{
40
    /**
41
     * @var        DatabaseManager An AgaviDatabaseManager instance.
42
     */
43
    protected $databaseManager = null;
44
    
45
    /**
46
     * @var        mixed A database connection.
47
     */
48
    protected $connection = null;
49
50
    /**
51
     * @var        string The name of the database.
52
     */
53
    private $name = null;
54
55
    /**
56
     * @var        mixed A database resource.
57
     */
58
    protected $resource = null;
59
60
    /**
61
     * Connect to the database.
62
     *
63
     * @throws     <b>AgaviDatabaseException</b> If a connection could not be
64
     *                                           created.
65
     *
66
     * @author     Sean Kerr <[email protected]>
67
     * @since      0.9.0
68
     */
69
    abstract protected function connect();
70
    
71
    /**
72
     * Retrieve the Database Manager instance for this implementation.
73
     *
74
     * @return     DatabaseManager A Database Manager instance.
75
     *
76
     * @author     David Zülke <[email protected]>
77
     * @since      0.11.0
78
     */
79
    public function getDatabaseManager()
80
    {
81
        return $this->databaseManager;
82
    }
83
84
    /**
85
     * Retrieve the name of this database connection.
86
     *
87
     * @return     string The name of the database.
88
     *
89
     * @author     David Zülke <[email protected]>
90
     * @since      0.11.0
91
     */
92
    public function getName()
93
    {
94
        return $this->name;
95
    }
96
    
97
    /**
98
     * Retrieve the database connection associated with this Database
99
     * implementation.
100
     *
101
     * When this is executed on a Database implementation that isn't an
102
     * abstraction layer, a copy of the resource will be returned.
103
     *
104
     * @return     mixed A database connection.
105
     *
106
     * @throws     DatabaseException If a connection could not be retrieved.
107
     *
108
     * @author     Sean Kerr <[email protected]>
109
     * @since      0.9.0
110
     */
111
    public function getConnection()
112
    {
113
        if ($this->connection === null) {
114
            $this->connect();
115
        }
116
117
        return $this->connection;
118
    }
119
120
    /**
121
     * Retrieve a raw database resource associated with this Database
122
     * implementation.
123
     *
124
     * @return     mixed A database resource.
125
     *
126
     * @throws     <b>AgaviDatabaseException</b> If no resource could be retrieved
127
     *
128
     * @author     Sean Kerr <[email protected]>
129
     * @since      0.9.0
130
     */
131
    public function getResource()
132
    {
133
        if ($this->resource === null) {
134
            $this->connect();
135
        }
136
137
        return $this->resource;
138
    }
139
140
    /**
141
     * Initialize this Database.
142
     *
143
     * @param      DatabaseManager $databaseManager The database manager of this instance.
144
     * @param      array           $parameters      An assoc array of initialization params.
145
     *
146
     * @throws     InitializationException If an error occurs while initializing this Database.
147
     *
148
     * @author     David Zülke <[email protected]>
149
     * @author     Sean Kerr <[email protected]>
150
     * @since      0.9.0
151
     */
152
    public function initialize(DatabaseManager $databaseManager, array $parameters = array())
153
    {
154
        $this->databaseManager = $databaseManager;
155
        
156
        $this->setParameters($parameters);
157
        
158
        $this->name = $databaseManager->getDatabaseName($this);
0 ignored issues
show
Documentation Bug introduced by
It seems like $databaseManager->getDatabaseName($this) can also be of type false or integer. However, the property $name is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
159
    }
160
161
    /**
162
     * Do any necessary startup work after initialization.
163
     *
164
     * This method is not called directly after initialize().
165
     * It is called during the startup() of the database manager.
166
     *
167
     * @author     David Zülke <[email protected]>
168
     * @since      0.11.0
169
     */
170
    public function startup()
171
    {
172
    }
173
174
    /**
175
     * Execute the shutdown procedure.
176
     *
177
     * @throws     <b>AgaviDatabaseException</b> If an error occurs while shutting
178
     *                                           down this database.
179
     *
180
     * @author     Sean Kerr <[email protected]>
181
     * @since      0.9.0
182
     */
183
    abstract public function shutdown();
184
}
185