Completed
Push — master ( 0607e7...c41fbf )
by
unknown
10s
created

Statistics::setCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 Doctrine\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 \Doctrine\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 \Doctrine\MongoDB\Collection
61
     */
62 4
    public function setCollection(Collection $collection)
63
    {
64 4
        $this->collection = $collection;
65 4
    }
66
67
    /**
68
     * @return \Doctrine\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 3
    protected function getCollectionStats()
117
    {
118 3
        $database = $this->collection->getDatabase();
119 3
        $name = $this->collection->getName();
120
121 3
        if (!$data = $database->command(['collStats' => $name])) {
122 1
            $message = sprintf(
123 1
                'Statistics not found for collection %s',
124 1
                $name
125
            );
126 1
            throw new Exception($message);
127
        }
128
129 2
        if (isset($data['errmsg'])) {
130 1
            throw new Exception($data['errmsg']);
131
        }
132
133 1
        return $data;
134
    }
135
}
136