Passed
Push — main ( 4889b2...808ce0 )
by Michiel
07:43
created

PDOTask::setCaching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\System\Pdo;
21
22
use Exception;
23
use PDO;
24
use PDOException;
25
use Phing\Exception\BuildException;
26
use Phing\Project;
27
use Phing\Task;
28
29
/**
30
 * Handles PDO configuration needed by SQL type tasks.
31
 *
32
 * @author  Hans Lellelid <[email protected]> (Phing)
33
 * @author  Nick Chalko <[email protected]> (Ant)
34
 * @author  Jeff Martin <[email protected]> (Ant)
35
 * @author  Michael McCallum <[email protected]> (Ant)
36
 * @author  Tim Stephenson <[email protected]> (Ant)
37
 */
38
abstract class PDOTask extends Task
39
{
40
    private $caching = true;
41
42
    /**
43
     * Autocommit flag. Default value is false
44
     */
45
    private $autocommit = false;
46
47
    /**
48
     * DB url.
49
     */
50
    private $url;
51
52
    /**
53
     * User name.
54
     */
55
    private $userId;
56
57
    /**
58
     * Password
59
     */
60
    private $password;
61
62
    /**
63
     * Initialize the PDOTask
64
     * This method checks if the PDO classes are available and triggers
65
     * appropriate error if they cannot be found.  This is not done in header
66
     * because we may want this class to be loaded w/o triggering an error.
67
     */
68
    public function init()
69
    {
70
        if (!class_exists('PDO')) {
71
            throw new Exception("PDOTask depends on PDO feature being included in PHP.");
72
        }
73
    }
74
75
    /**
76
     * Caching loaders / driver. This is to avoid
77
     * getting an OutOfMemoryError when calling this task
78
     * multiple times in a row; default: true
79
     *
80
     * @param bool $caching
81
     */
82
    public function setCaching($caching)
83
    {
84
        $this->caching = $caching;
85
    }
86
87
    /**
88
     * Sets the database connection URL; required.
89
     *
90
     * @param string $url The url to set
91
     */
92 6
    public function setUrl($url)
93
    {
94 6
        $this->url = $url;
95 6
    }
96
97
    /**
98
     * Sets the password; required.
99
     *
100
     * @param string $password The password to set
101
     */
102
    public function setPassword($password)
103
    {
104
        $this->password = $password;
105
    }
106
107
    /**
108
     * Auto commit flag for database connection;
109
     * optional, default false.
110
     *
111
     * @param bool $autocommit The autocommit to set
112
     */
113 3
    public function setAutocommit($autocommit)
114
    {
115 3
        $this->autocommit = $autocommit;
116 3
    }
117
118
    /**
119
     * Creates a new Connection as using the driver, url, userid and password specified.
120
     * The calling method is responsible for closing the connection.
121
     *
122
     * @return PDO     the newly created connection.
123
     * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver
124
     *                        or the driver fails to load.
125
     */
126
    protected function getConnection()
127
    {
128
        if ($this->url === null) {
129
            throw new BuildException("Url attribute must be set!", $this->getLocation());
130
        }
131
132
        try {
133
            $this->log("Connecting to " . $this->getUrl(), Project::MSG_VERBOSE);
134
135
            $user = null;
136
            $pass = null;
137
138
            if ($this->userId) {
139
                $user = $this->getUserId();
140
            }
141
142
            if ($this->password) {
143
                $pass = $this->getPassword();
144
            }
145
146
            $conn = new PDO($this->getUrl(), $user, $pass);
147
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
148
149
            try {
150
                $conn->setAttribute(PDO::ATTR_AUTOCOMMIT, $this->autocommit);
151
            } catch (PDOException $pe) {
152
                $this->log(
153
                    "Unable to enable auto-commit for this database: " . $pe->getMessage(),
154
                    Project::MSG_VERBOSE
155
                );
156
            }
157
158
            return $conn;
159
        } catch (PDOException $e) {
160
            throw new BuildException($e->getMessage(), $this->getLocation());
161
        }
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isCaching()
168
    {
169
        return $this->caching;
170
    }
171
172
    /**
173
     * Gets the autocommit.
174
     *
175
     * @return bool
176
     */
177 3
    public function isAutocommit()
178
    {
179 3
        return $this->autocommit;
180
    }
181
182
    /**
183
     * Gets the url.
184
     *
185
     * @return string
186
     */
187 8
    public function getUrl()
188
    {
189 8
        return $this->url;
190
    }
191
192
    /**
193
     * Gets the userId.
194
     *
195
     * @return string
196
     */
197 5
    public function getUserId()
198
    {
199 5
        return $this->userId;
200
    }
201
202
    /**
203
     * Set the user name for the connection; required.
204
     *
205
     * @param string $userId
206
     */
207 1
    public function setUserid($userId)
208
    {
209 1
        $this->userId = $userId;
210 1
    }
211
212
    /**
213
     * Gets the password.
214
     *
215
     * @return string
216
     */
217 5
    public function getPassword()
218
    {
219 5
        return $this->password;
220
    }
221
}
222