MysqlProfilerStorage   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildCriteria() 0 31 6
A initDb() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sitetheory\Bundle\ProfilerStorageBundle\Profiler;
13
14
/**
15
 * A ProfilerStorage for Mysql.
16
 *
17
 * Class MysqlProfilerStorage
18
 *
19
 * @author Jan Schumann <[email protected]>
20
 */
21
class MysqlProfilerStorage extends PdoProfilerStorage
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function initDb()
27
    {
28
        if (null === $this->db) {
29
            if (0 !== strpos($this->dsn, 'mysql')) {
30
                throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Mysql with an invalid dsn "%s". The expected format is "mysql:dbname=database_name;host=host_name".', $this->dsn));
31
            }
32
33
            if (!class_exists('PDO') || !in_array('mysql', \PDO::getAvailableDrivers(), true)) {
34
                throw new \RuntimeException('You need to enable PDO_Mysql extension for the profiler to run properly.');
35
            }
36
37
            $db = new \PDO($this->dsn, $this->username, $this->password);
38
            $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url TEXT, time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, status_code SMALLINT UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url(192)), KEY (parent))');
39
40
            $this->db = $db;
41
        }
42
43
        return $this->db;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
50
    {
51
        $criteria = array();
52
        $args = array();
53
54
        if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
55
            $criteria[] = 'ip LIKE :ip';
56
            $args[':ip'] = '%'.$ip.'%';
57
        }
58
59
        if ($url) {
60
            $criteria[] = 'url LIKE :url';
61
            $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
62
        }
63
64
        if ($method) {
65
            $criteria[] = 'method = :method';
66
            $args[':method'] = $method;
67
        }
68
69
        if (!empty($start)) {
70
            $criteria[] = 'time >= :start';
71
            $args[':start'] = $start;
72
        }
73
74
        if (!empty($end)) {
75
            $criteria[] = 'time <= :end';
76
            $args[':end'] = $end;
77
        }
78
79
        return array($criteria, $args);
80
    }
81
}
82