Completed
Pull Request — master (#40)
by Monse
02:20
created

PdoHandler::createTable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.4285
cc 3
eloc 13
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Noxlogic\RateLimitBundle\Entity;
4
5
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
6
7
class PdoHandler extends PdoSessionHandler
8
{
9
    /**
10
     * No locking is done. This means sessions are prone to loss of data due to
11
     * race conditions of concurrent requests to the same session. The last session
12
     * write will win in this case. It might be useful when you implement your own
13
     * logic to deal with this like an optimistic approach.
14
     */
15
    const LOCK_NONE = 0;
16
17
    /**
18
     * Creates an application-level lock on a session. The disadvantage is that the
19
     * lock is not enforced by the database and thus other, unaware parts of the
20
     * application could still concurrently modify the session. The advantage is it
21
     * does not require a transaction.
22
     * This mode is not available for SQLite and not yet implemented for oci and sqlsrv.
23
     */
24
    const LOCK_ADVISORY = 1;
25
26
    /**
27
     * Issues a real row lock. Since it uses a transaction between opening and
28
     * closing a session, you have to be careful when you use same database connection
29
     * that you also use for your application logic. This mode is the default because
30
     * it's the only reliable solution across DBMSs.
31
     */
32
    const LOCK_TRANSACTIONAL = 2;
33
34
    /**
35
     * @var \PDO|null PDO instance or null when not connected yet
36
     */
37
    private $pdo;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
38
39
    /**
40
     * @var string|null|false DSN string or null for session.save_path or false when lazy connection disabled
41
     */
42
    private $dsn = false;
43
44
    /**
45
     * @var string Database driver
46
     */
47
    private $driver;
48
49
    /**
50
     * @var string Table name
51
     */
52
    private $table;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
53
54
    /**
55
     * @var string Column for cache id
56
     */
57
    private $idCol;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
58
59
    /**
60
     * @var string Column for cache data
61
     */
62
    private $dataCol;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
63
64
    /**
65
     * @var string Column for lifetime
66
     */
67
    private $lifetimeCol;
68
69
    /**
70
     * @var string Column for timestamp
71
     */
72
    private $timeCol;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
73
74
    /**
75
     * @var string Username when lazy-connect
76
     */
77
    private $username = '';
78
79
    /**
80
     * @var string Password when lazy-connect
81
     */
82
    private $password = '';
83
84
    /**
85
     * @var array Connection options when lazy-connect
86
     */
87
    private $connectionOptions = array();
88
89
    /**
90
     * @var int The strategy for locking, see constants
91
     */
92
    private $lockMode = self::LOCK_TRANSACTIONAL;
93
94
    public function __construct($pdoOrDsn = null, array $options = array())
95
    {
96
        if ($pdoOrDsn instanceof \PDO) {
97
            if (\PDO::ERRMODE_EXCEPTION !== $pdoOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) {
98
                throw new \InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION))', __CLASS__));
99
            }
100
101
            $this->pdo = $pdoOrDsn;
102
            $this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
103
        } else {
104
            $this->dsn = $pdoOrDsn;
105
        }
106
107
        $this->table = isset($options['db_table']) ? $options['db_table'] : $this->table;
108
        $this->idCol = isset($options['db_id_col']) ? $options['db_id_col'] : $this->idCol;
109
        $this->dataCol = isset($options['db_data_col']) ? $options['db_data_col'] : $this->dataCol;
110
        $this->lifetimeCol = isset($options['db_lifetime_col']) ? $options['db_lifetime_col'] : $this->lifetimeCol;
111
        $this->timeCol = isset($options['db_time_col']) ? $options['db_time_col'] : $this->timeCol;
112
        $this->username = isset($options['db_username']) ? $options['db_username'] : $this->username;
113
        $this->password = isset($options['db_password']) ? $options['db_password'] : $this->password;
114
        $this->connectionOptions = isset($options['db_connection_options']) ? $options['db_connection_options'] : $this->connectionOptions;
115
        $this->lockMode = isset($options['lock_mode']) ? $options['lock_mode'] : $this->lockMode;
116
    }
117
118
    public function createTable()
119
    {
120
        $this->getConnection();
0 ignored issues
show
Unused Code introduced by
The call to the method Noxlogic\RateLimitBundle...andler::getConnection() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
121
122
        switch ($this->driver) {
123
            case 'pgsql':
124
                $sql = "CREATE TABLE IF NOT EXISTS database_cache ( id VARCHAR(128) NOT NULL PRIMARY KEY, limit INTEGER NOT NULL, info VARCHAR(255) NOT NULL, period INTEGER NOT NULL, reset INTEGER NOT NULL )";
125
                break;
126
            default:
127
                throw new \DomainException(sprintf('Creating the database cache table is currently not implemented for PDO driver "%s".', $this->driver));
128
        }
129
130
        try {
131
            $this->pdo->exec($sql);
132
        } catch (\PDOException $e) {
133
            $this->rollback();
0 ignored issues
show
Bug introduced by
The method rollback() does not seem to exist on object<Noxlogic\RateLimi...ndle\Entity\PdoHandler>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
135
            throw $e;
136
        }
137
    }
138
139
    public function fetch($key){
140
        $this->read($key);
141
    }
142
143
    public function save($key, $info){
144
        $this->write($key, $info);
145
    }
146
147
    public function delete($key){
148
        $this->destroy($key);
149
    }
150
}