MysqlSessionInit::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Event\Listeners;
21
22
use Doctrine\DBAL\Event\ConnectionEventArgs;
23
use Doctrine\DBAL\Events;
24
use Doctrine\Common\EventSubscriber;
25
26
/**
27
 * MySQL Session Init Event Subscriber which allows to set the Client Encoding of the Connection.
28
 *
29
 * @link       www.doctrine-project.org
30
 * @since      1.0
31
 * @author     Benjamin Eberlei <[email protected]>
32
 * @deprecated Use "charset" option to PDO MySQL Connection instead.
33
 */
34
class MysqlSessionInit implements EventSubscriber
35
{
36
    /**
37
     * The charset.
38
     *
39
     * @var string
40
     */
41
    private $_charset;
42
43
    /**
44
     * The collation, or FALSE if no collation.
45
     *
46
     * @var string|boolean
47
     */
48
    private $_collation;
49
50
    /**
51
     * Configure Charset and Collation options of MySQL Client for each Connection.
52
     *
53
     * @param string         $charset   The charset.
54
     * @param string|boolean $collation The collation, or FALSE if no collation.
55
     */
56
    public function __construct($charset = 'utf8', $collation = false)
57
    {
58
        $this->_charset = $charset;
59
        $this->_collation = $collation;
60
    }
61
62
    /**
63
     * @param \Doctrine\DBAL\Event\ConnectionEventArgs $args
64
     *
65
     * @return void
66
     */
67
    public function postConnect(ConnectionEventArgs $args)
68
    {
69
        $collation = ($this->_collation) ? " COLLATE ".$this->_collation : "";
70
        $args->getConnection()->executeUpdate("SET NAMES ".$this->_charset . $collation);
0 ignored issues
show
Security introduced by
If $collation can contain user-input, it is usually preferable to use a parameter placeholder like :paramName and pass the dynamic input as second argument array('param' => $collation).

Instead of embedding dynamic parameters in SQL, Doctrine also allows you to pass them separately and insert a placeholder instead:

function findUser(Doctrine\DBAL\Connection $con, $email) {
    // Unsafe
    $con->executeQuery("SELECT * FROM users WHERE email = '".$email."'");

    // Safe
    $con->executeQuery(
        "SELECT * FROM users WHERE email = :email",
        array('email' => $email)
    );
}
Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getSubscribedEvents()
77
    {
78
        return [Events::postConnect];
79
    }
80
}
81