Passed
Push — main ( ddd1a4...b98cb8 )
by Michiel
08:47
created

PDOTask::setUserid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
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
 * @package phing.tasks.system
38
 */
39
abstract class PDOTask extends Task
40
{
41
    private $caching = true;
42
43
    /**
44
     * Autocommit flag. Default value is false
45
     */
46
    private $autocommit = false;
47
48
    /**
49
     * DB url.
50
     */
51
    private $url;
52
53
    /**
54
     * User name.
55
     */
56
    private $userId;
57
58
    /**
59
     * Password
60
     */
61
    private $password;
62
63
    /**
64
     * Initialize the PDOTask
65
     * This method checks if the PDO classes are available and triggers
66
     * appropriate error if they cannot be found.  This is not done in header
67
     * because we may want this class to be loaded w/o triggering an error.
68
     */
69
    public function init()
70
    {
71
        if (!class_exists('PDO')) {
72
            throw new Exception("PDOTask depends on PDO feature being included in PHP.");
73
        }
74
    }
75
76
    /**
77
     * Caching loaders / driver. This is to avoid
78
     * getting an OutOfMemoryError when calling this task
79
     * multiple times in a row; default: true
80
     *
81
     * @param $enable
82
     */
83
    public function setCaching($enable)
84
    {
85
        $this->caching = $enable;
86
    }
87
88
    /**
89
     * Sets the database connection URL; required.
90
     *
91
     * @param string The url to set
0 ignored issues
show
Bug introduced by
The type Phing\Task\System\Pdo\The 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...
92
     */
93 6
    public function setUrl($url)
94
    {
95 6
        $this->url = $url;
96 6
    }
97
98
    /**
99
     * Sets the password; required.
100
     *
101
     * @param string $password The password to set
102
     */
103
    public function setPassword($password)
104
    {
105
        $this->password = $password;
106
    }
107
108
    /**
109
     * Auto commit flag for database connection;
110
     * optional, default false.
111
     *
112
     * @param bool $autocommit The autocommit to set
113
     */
114 3
    public function setAutocommit($autocommit)
115
    {
116 3
        $this->autocommit = $autocommit;
117 3
    }
118
119
    /**
120
     * Creates a new Connection as using the driver, url, userid and password specified.
121
     * The calling method is responsible for closing the connection.
122
     *
123
     * @return PDO     the newly created connection.
124
     * @throws BuildException if the UserId/Password/Url is not set or there is no suitable driver
125
     *                        or the driver fails to load.
126
     */
127
    protected function getConnection()
128
    {
129
        if ($this->url === null) {
130
            throw new BuildException("Url attribute must be set!", $this->getLocation());
131
        }
132
133
        try {
134
            $this->log("Connecting to " . $this->getUrl(), Project::MSG_VERBOSE);
135
136
            $user = null;
137
            $pass = null;
138
139
            if ($this->userId) {
140
                $user = $this->getUserId();
141
            }
142
143
            if ($this->password) {
144
                $pass = $this->getPassword();
145
            }
146
147
            $conn = new PDO($this->getUrl(), $user, $pass);
148
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
149
150
            try {
151
                $conn->setAttribute(PDO::ATTR_AUTOCOMMIT, $this->autocommit);
152
            } catch (PDOException $pe) {
153
                $this->log(
154
                    "Unable to enable auto-commit for this database: " . $pe->getMessage(),
155
                    Project::MSG_VERBOSE
156
                );
157
            }
158
159
            return $conn;
160
        } catch (PDOException $e) {
161
            throw new BuildException($e->getMessage(), $this->getLocation());
162
        }
163
    }
164
165
    /**
166
     * @param $value
167
     */
168
    public function isCaching($value)
169
    {
170
        $this->caching = $value;
171
    }
172
173
    /**
174
     * Gets the autocommit.
175
     *
176
     * @return bool
177
     */
178 3
    public function isAutocommit()
179
    {
180 3
        return $this->autocommit;
181
    }
182
183
    /**
184
     * Gets the url.
185
     *
186
     * @return string
187
     */
188 8
    public function getUrl()
189
    {
190 8
        return $this->url;
191
    }
192
193
    /**
194
     * Gets the userId.
195
     *
196
     * @return string
197
     */
198 5
    public function getUserId()
199
    {
200 5
        return $this->userId;
201
    }
202
203
    /**
204
     * Set the user name for the connection; required.
205
     *
206
     * @param string $userId
207
     */
208 1
    public function setUserid($userId)
209
    {
210 1
        $this->userId = $userId;
211 1
    }
212
213
    /**
214
     * Gets the password.
215
     *
216
     * @return string
217
     */
218 5
    public function getPassword()
219
    {
220 5
        return $this->password;
221
    }
222
}
223