Completed
Push — master ( 3a9f70...7b04fe )
by Philip
01:59
created

DBAL   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildStatements() 0 22 1
A __construct() 0 5 1
A storeMeasurement() 0 10 2
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 function buildStatements() {
29
        $queryBuilder             = $this->connection->createQueryBuilder()
30
            ->select('COUNT(`key`) AS amount')
31
            ->from('`'.$this->table.'`')
32
            ->where('`key` = ?')
33
        ;
34
        $this->statementKeyExists = $this->connection->prepare($queryBuilder->getSQL());
35
36
        $queryBuilder             = $this->connection->createQueryBuilder()
37
            ->insert('`'.$this->table.'`')
38
            ->setValue('`value`', '?')
39
            ->setValue('`key`', '?')
40
        ;
41
        $this->statementKeyInsert = $this->connection->prepare($queryBuilder->getSQL());
42
43
        $queryBuilder             = $this->connection->createQueryBuilder()
44
            ->update('`'.$this->table.'`')
45
            ->set('`value`', '?')
46
            ->where('`key` = ?')
47
        ;
48
        $this->statementKeyUpdate = $this->connection->prepare($queryBuilder->getSQL());
49
    }
50
51
    public function __construct(Connection $connection, $table = 'phpprom') {
52
        $this->connection = $connection;
53
        $this->table      = $table;
54
        $this->buildStatements();
55
    }
56
57
    public function storeMeasurement($prefix, $key, $value) {
58
        $prefixedKey = $prefix.':'.$key;
59
        $this->statementKeyExists->bindValue(1, $prefixedKey);
60
        $this->statementKeyExists->execute();
61
        $exists         = $this->statementKeyExists->fetch(\PDO::FETCH_ASSOC);
62
        $statementStore = $exists['amount'] > 0 ? $this->statementKeyUpdate : $this->statementKeyInsert;
63
        $statementStore->bindValue(1, $value);
64
        $statementStore->bindValue(2, $prefixedKey);
65
        $statementStore->execute();
66
    }
67
68
    public function getMeasurements($prefix, array $keys) {
69
        $prefixedKeys = array_map(function($key) use ($prefix) {
70
            return $prefix.':'.$key;
71
        }, $keys);
72
        $queryBuilder = $this->connection->createQueryBuilder();
73
        $queryBuilder
74
            ->select('`key`', '`value`')
75
            ->from('`'.$this->table.'`')
76
            ->where('`key` IN (?)')
77
            ->setParameter(1, $prefixedKeys, Connection::PARAM_STR_ARRAY)
78
        ;
79
        $measurements = [];
80
        foreach ($keys as $key) {
81
            $measurements[$key] = 'Nan';
82
        }
83
        $rows = $queryBuilder->execute()->fetchAll(\PDO::FETCH_ASSOC);
84
        foreach ($rows as $row) {
85
            $unprefixedKey                = substr($row['key'], strlen($prefix) + 1);
86
            $measurements[$unprefixedKey] = (float)$row['value'];
87
        }
88
        return $measurements;
89
    }
90
}
91