Passed
Pull Request — master (#43)
by
unknown
16:58
created

Statistics::getCollectionStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
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 AntiMattr\MongoDB\Migrations\Collection;
13
14
use \MongoDB\Collection;
0 ignored issues
show
Bug introduced by
The type MongoDB\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Exception;
16
use MongoDB\Database;
0 ignored issues
show
Bug introduced by
The type MongoDB\Database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * @author Matthew Fitzgerald <[email protected]>
20
 */
21
class Statistics
22
{
23
    const COUNT = 'count';
24
    const SIZE = 'size';
25
    const AVG_OBJ_SIZE = 'avgObjSize';
26
    const STORAGE_SIZE = 'storageSize';
27
    const NUM_EXTENTS = 'numExtents';
28
    const NINDEXES = 'nindexes';
29
    const LAST_EXTENT_SIZE = 'lastExtentSize';
30
    const PADDING_FACTOR = 'paddingFactor';
31
    const TOTAL_INDEX_SIZE = 'totalIndexSize';
32
33
    public static $metrics = [
34
        self::COUNT,
35
        self::SIZE,
36
        self::AVG_OBJ_SIZE,
37
        self::STORAGE_SIZE,
38
        self::NUM_EXTENTS,
39
        self::NINDEXES,
40
        self::LAST_EXTENT_SIZE,
41
        self::PADDING_FACTOR,
42
        self::TOTAL_INDEX_SIZE,
43
    ];
44
45
    /**
46
     * @var \MongoDB\Collection
47
     */
48
    private $collection;
49
50
    /**
51
     * @var array
52
     */
53
    private $before = [];
54
55
    /**
56
     * @var array
57
     */
58
    private $after = [];
59
60
    public function setDatabase(Database $database)
61
    {
62 4
        $this->database = $database;
0 ignored issues
show
Bug Best Practice introduced by
The property database does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63
    }
64 4
65 4
    /**
66
     * @param \MongoDB\Collection
67
     */
68
    public function setCollection(Collection $collection)
69
    {
70 1
        $this->collection = $collection;
71
    }
72 1
73
    /**
74
     * @return \MongoDB\Collection
75
     */
76
    public function getCollection()
77
    {
78
        return $this->collection;
79
    }
80
81
    public function updateBefore()
82
    {
83
        $data = $this->getCollectionStats();
84
        foreach ($data as $key => $value) {
85
            if (in_array($key, static::$metrics)) {
86
                $this->before[$key] = $value;
87
            }
88
        }
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getBefore()
95
    {
96
        return $this->before;
97
    }
98
99
    public function updateAfter()
100
    {
101
        $data = $this->getCollectionStats();
102
        foreach ($data as $key => $value) {
103
            if (in_array($key, static::$metrics)) {
104
                $this->after[$key] = $value;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getAfter()
113
    {
114
        return $this->after;
115
    }
116 3
117
    /**
118 3
     * @return array
119 3
     *
120
     * @throws \Exception
121 3
     */
122 1
    protected function getCollectionStats()
123 1
    {
124 1
        $name = $this->collection->getCollectionName();
125
126 1
        if (!$data = $this->database->command(['collStats' => $name])) {
127
            $message = sprintf(
128
                'Statistics not found for collection %s',
129 2
                $name
130 1
            );
131
            throw new Exception($message);
132
        }
133 1
134
        return $data;
135
    }
136
}
137