Passed
Pull Request — master (#41)
by
unknown
03:10
created

Statistics   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 15.63%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 114
ccs 5
cts 32
cp 0.1563
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setCollection() 0 3 1
A getCollection() 0 3 1
A getBefore() 0 3 1
A getAfter() 0 3 1
A updateAfter() 0 6 3
A updateBefore() 0 6 3
A getCollectionStats() 0 18 3
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;
15
use Exception;
16
17
/**
18
 * @author Matthew Fitzgerald <[email protected]>
19
 */
20
class Statistics
21
{
22
    const COUNT = 'count';
23
    const SIZE = 'size';
24
    const AVG_OBJ_SIZE = 'avgObjSize';
25
    const STORAGE_SIZE = 'storageSize';
26
    const NUM_EXTENTS = 'numExtents';
27
    const NINDEXES = 'nindexes';
28
    const LAST_EXTENT_SIZE = 'lastExtentSize';
29
    const PADDING_FACTOR = 'paddingFactor';
30
    const TOTAL_INDEX_SIZE = 'totalIndexSize';
31
32
    public static $metrics = [
33
        self::COUNT,
34
        self::SIZE,
35
        self::AVG_OBJ_SIZE,
36
        self::STORAGE_SIZE,
37
        self::NUM_EXTENTS,
38
        self::NINDEXES,
39
        self::LAST_EXTENT_SIZE,
40
        self::PADDING_FACTOR,
41
        self::TOTAL_INDEX_SIZE,
42
    ];
43
44
    /**
45
     * @var \MongoDB\Collection
46
     */
47
    private $collection;
48
49
    /**
50
     * @var array
51
     */
52
    private $before = [];
53
54
    /**
55
     * @var array
56
     */
57
    private $after = [];
58
59
    /**
60
     * @param \MongoDB\Collection
61
     */
62 3
    public function setCollection(Collection $collection)
63
    {
64 3
        $this->collection = $collection;
65 3
    }
66
67
    /**
68
     * @return \MongoDB\Collection
69
     */
70 1
    public function getCollection()
71
    {
72 1
        return $this->collection;
73
    }
74
75
    public function updateBefore()
76
    {
77
        $data = $this->getCollectionStats();
78
        foreach ($data as $key => $value) {
79
            if (in_array($key, static::$metrics)) {
80
                $this->before[$key] = $value;
81
            }
82
        }
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getBefore()
89
    {
90
        return $this->before;
91
    }
92
93
    public function updateAfter()
94
    {
95
        $data = $this->getCollectionStats();
96
        foreach ($data as $key => $value) {
97
            if (in_array($key, static::$metrics)) {
98
                $this->after[$key] = $value;
99
            }
100
        }
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getAfter()
107
    {
108
        return $this->after;
109
    }
110
111
    /**
112
     * @return array
113
     *
114
     * @throws \Exception
115
     */
116
    protected function getCollectionStats()
117
    {
118
        $database = $this->collection->getDatabase();
0 ignored issues
show
Bug introduced by
The method getDatabase() does not exist on MongoDB\Collection. Did you maybe mean getDatabaseName()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        /** @scrutinizer ignore-call */ 
119
        $database = $this->collection->getDatabase();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
        $name = $this->collection->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on MongoDB\Collection. Did you maybe mean getNamespace()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        /** @scrutinizer ignore-call */ 
120
        $name = $this->collection->getName();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
121
        if (!$data = $database->command(['collStats' => $name])) {
122
            $message = sprintf(
123
                'Statistics not found for collection %s',
124
                $name
125
            );
126
            throw new Exception($message);
127
        }
128
129
        if (isset($data['errmsg'])) {
130
            throw new Exception($data['errmsg']);
131
        }
132
133
        return $data;
134
    }
135
}
136