Completed
Push — master ( 7b04fe...bd89c1 )
by Philip
02:01
created

DBAL   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 99
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B buildStatements() 0 29 1
A __construct() 0 5 1
A storeMeasurement() 0 10 2
A incrementMeasurement() 0 14 3
A getMeasurements() 0 22 3
1
<?php
2
3
/*
4
 * This file is part of the PHPProm package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[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 PHPProm\Storage;
13
14
use \Doctrine\DBAL\Connection;
15
16
class DBAL implements StorageInterface {
17
18
    protected $connection;
19
20
    protected $table;
21
22
    protected $statementKeyExists;
23
24
    protected $statementKeyInsert;
25
26
    protected $statementKeyUpdate;
27
28
    protected $statementKeyIncrement;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $statementKeyIncrement exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
29
30
    protected function buildStatements() {
31
        $queryBuilder                = $this->connection->createQueryBuilder()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 16 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
            ->select('COUNT(`key`) AS amount')
33
            ->from('`'.$this->table.'`')
34
            ->where('`key` = ?')
35
        ;
36
        $this->statementKeyExists    = $this->connection->prepare($queryBuilder->getSQL());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
37
38
        $queryBuilder                = $this->connection->createQueryBuilder()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 16 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
            ->insert('`'.$this->table.'`')
40
            ->setValue('`value`', '?')
41
            ->setValue('`key`', '?')
42
        ;
43
        $this->statementKeyInsert    = $this->connection->prepare($queryBuilder->getSQL());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
45
        $queryBuilder                = $this->connection->createQueryBuilder()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 16 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
            ->update('`'.$this->table.'`')
47
            ->set('`value`', '?')
48
            ->where('`key` = ?')
49
        ;
50
        $this->statementKeyUpdate    = $this->connection->prepare($queryBuilder->getSQL());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 4 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
52
        $queryBuilder                = $this->connection->createQueryBuilder()
53
            ->update('`'.$this->table.'`')
54
            ->set('`value`', '`value` + 1')
55
            ->where('`key` = ?')
56
        ;
57
        $this->statementKeyIncrement = $this->connection->prepare($queryBuilder->getSQL());
58
    }
59
60
    public function __construct(Connection $connection, $table = 'phpprom') {
61
        $this->connection = $connection;
62
        $this->table      = $table;
63
        $this->buildStatements();
64
    }
65
66
    public function storeMeasurement($prefix, $key, $value) {
67
        $prefixedKey = $prefix.':'.$key;
68
        $this->statementKeyExists->bindValue(1, $prefixedKey);
69
        $this->statementKeyExists->execute();
70
        $exists         = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC);
71
        $statementStore = $exists['amount'] > 0 ? $this->statementKeyUpdate : $this->statementKeyInsert;
72
        $statementStore->bindValue(1, $value);
73
        $statementStore->bindValue(2, $prefixedKey);
74
        $statementStore->execute();
75
    }
76
77
    public function incrementMeasurement($prefix, $key) {
78
        $prefixedKey = $prefix.':'.$key;
79
        $this->statementKeyExists->bindValue(1, $prefixedKey);
80
        $this->statementKeyExists->execute();
81
        $exists             = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC);
82
        $statementIncrement = $exists['amount'] > 0 ? $this->statementKeyIncrement : $this->statementKeyInsert;
83
        if ($exists['amount'] > 0) {
84
            $statementIncrement->bindValue(1, $prefixedKey);
85
        } else {
86
            $statementIncrement->bindValue(1, 1);
87
            $statementIncrement->bindValue(2, $prefixedKey);
88
        }
89
        $statementIncrement->execute();
90
    }
91
92
    public function getMeasurements($prefix, array $keys, $defaultValue = 'Nan') {
93
        $prefixedKeys = array_map(function($key) use ($prefix) {
94
            return $prefix.':'.$key;
95
        }, $keys);
96
        $queryBuilder = $this->connection->createQueryBuilder();
97
        $queryBuilder
98
            ->select('`key`', '`value`')
99
            ->from('`'.$this->table.'`')
100
            ->where('`key` IN (?)')
101
            ->setParameter(1, $prefixedKeys, Connection::PARAM_STR_ARRAY)
102
        ;
103
        $measurements = [];
104
        foreach ($keys as $key) {
105
            $measurements[$key] = $defaultValue;
106
        }
107
        $rows = $queryBuilder->execute()->fetchAll(\PDO::FETCH_ASSOC);
108
        foreach ($rows as $row) {
109
            $unprefixedKey                = substr($row['key'], strlen($prefix) + 1);
110
            $measurements[$unprefixedKey] = (float)$row['value'];
111
        }
112
        return $measurements;
113
    }
114
}
115