Completed
Push — master ( eb3b73...be30a5 )
by Philip
02:02
created

MongoDB::incrementMeasurement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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
/**
15
 * Class MongoDB
16
 * Storage implementation using MongoDB.
17
 * @package PHPProm\Storage
18
 */
19
class MongoDB extends AbstractStorage {
20
21
    /**
22
     * @var \MongoDB\Driver\Manager
23
     * The MongoDB Driver Manager.
24
     */
25
    protected $mongoDBManager;
26
27
    /**
28
     * @var string
29
     * The database name for the data.
30
     */
31
    protected $database;
32
33
    /**
34
     * @var string
35
     * The collection name for the data.
36
     */
37
    protected $collection;
38
39
    /**
40
     * MongoDB constructor.
41
     *
42
     * @param string $host
43
     * a mongodb:// connection URI
44
     * @param string $database
45
     * the database to use, defaults to "phppromdb"
46
     * @param string $collection
47
     * the collection to use, defaults to "measurements"
48
     * @param array $options
49
     * connection string options, defaults to []
50
     * @param array $driverOptions
51
     * any driver-specific options not included in MongoDB connection spec, defaults to []
52
     */
53
    public function __construct($host, $database = 'phppromdb', $collection = 'measurements', array $options = [], array $driverOptions = []) {
54
        parent::__construct();
55
        $this->mongoDBManager = new \MongoDB\Driver\Manager($host, $options, $driverOptions);
56
        $this->database = $database;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

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...
57
        $this->collection = $collection;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

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...
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function storeMeasurement($metric, $key, $value) {
64
        $bulkWrite = new \MongoDB\Driver\BulkWrite;
65
        $document = ['key' => $metric.':'.$key, 'value' => $value];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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...
66
        $filter = ['key' => $metric.':'.$key];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

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...
67
        $bulkWrite->update($filter, $document, ['upsert' => true]);
68
        $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
69
        $this->mongoDBManager->executeBulkWrite($this->database.'.'.$this->collection, $bulkWrite, $writeConcern);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function incrementMeasurement($metric, $key) {
76
        $filter = ['key' => $metric.':'.$key];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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...
77
        $options = ['limit' => 1];
78
        $query = new \MongoDB\Driver\Query($filter, $options);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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...
79
        $results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query);
80
        $value = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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...
81
        foreach ($results as $result) {
82
            $value += $result->value;
83
            break;
84
        }
85
        $this->storeMeasurement($metric, $key, $value);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function getMeasurements($metric, array $keys, $defaultValue = 'Nan') {
92
        $prefixedKeys = array_map(function($key) use ($metric) {
93
            return $metric.':'.$key;
94
        }, $keys);
95
96
        $measurements = [];
97
        foreach ($keys as $key) {
98
            $measurements[$key] = $defaultValue;
99
        }
100
        $filter = ['key' => ['$in' => $prefixedKeys]];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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...
101
        $query = new \MongoDB\Driver\Query($filter);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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...
102
        $results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query);
103
        foreach ($results as $result) {
104
            $unprefixedKey                = substr($result->key, strlen($metric) + 1);
105
            $measurements[$unprefixedKey] = (float)$result->value;
106
        }
107
        return $measurements;
108
    }
109
}
110