XhguiRuns   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 6.51%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 79
ccs 3
cts 46
cp 0.0651
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A get_run() 0 3 1
A save_run() 0 17 4
B prepareForSave() 0 35 6
1
<?php
2
3
namespace Jns\Bundle\XhprofBundle\Document;
4
5
use iXHProfRuns;
6
use Doctrine\ODM\MongoDB\DocumentManager;
7
8
class XhguiRuns implements iXHProfRuns
9
{
10
    /**
11
     * @var DocumentManager
12
     */
13
    private $documentManager;
14
15 1
    public function __construct(DocumentManager $documentManager) {
16 1
        $this->documentManager = $documentManager;
17 1
    }
18
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function get_run($run_id, $type, &$run_desc) {
23
        throw new \Exception('not implemented');
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function save_run($xhprof_data, $type, $run_id = null) {
30
        if (!class_exists('\Xhgui_Profiles') || !class_exists('\Xhgui_Saver_Mongo')) {
31
            throw new \Exception('composer require perftools/xhgui dev-master');
32
        }
33
        $data = $this->prepareForSave($xhprof_data);
34
        $dbname = $this->documentManager->getConfiguration()->getDefaultDB();
35
        $mongo = $this->documentManager->getConnection()->getMongoClient()->selectDB($dbname);
36
        $profiles = new \Xhgui_Profiles($mongo);
37
        $saver = new \Xhgui_Saver_Mongo($profiles);
38
        try {
39
            $saver->save($data);
40
            $run_id = (string)$data['_id'];
41
        } catch (\Exception $e) {
42
            error_log($e->getMessage());
43
        }
44
        return $run_id;
45
    }
46
47
    /**
48
     * @see https://github.com/perftools/xhgui/blob/ad532c42e55cf8b3413b8d7a2241eea1140b537f/external/header.php#L88
49
     * @todo instead of this copy pasta, refactor the perftools/xhgui side of things then reuse here
50
     */
51
    private function prepareForSave($xhprof_data) {
52
        $data = array('profile' => $xhprof_data);
53
        $uri = array_key_exists('REQUEST_URI', $_SERVER)
54
            ? $_SERVER['REQUEST_URI']
55
            : null;
56
        if (empty($uri) && isset($_SERVER['argv'])) {
57
            $cmd = basename($_SERVER['argv'][0]);
58
            $uri = $cmd . ' ' . implode(' ', array_slice($_SERVER['argv'], 1));
59
        }
60
        $time = array_key_exists('REQUEST_TIME', $_SERVER)
61
            ? $_SERVER['REQUEST_TIME']
62
            : time();
63
        $requestTimeFloat = explode('.', $_SERVER['REQUEST_TIME_FLOAT']);
64
        if (!isset($requestTimeFloat[1])) {
65
            $requestTimeFloat[1] = 0;
66
        }
67
        // if (Xhgui_Config::read('save.handler') === 'file') {
68
        //     $requestTs = array('sec' => $time, 'usec' => 0);
69
        //     $requestTsMicro = array('sec' => $requestTimeFloat[0], 'usec' => $requestTimeFloat[1]);
70
        // } else {
71
            $requestTs = new \MongoDate($time);
72
            $requestTsMicro = new \MongoDate($requestTimeFloat[0], $requestTimeFloat[1]);
73
        // }
74
        $data['meta'] = array(
75
            'url' => $uri,
76
            'SERVER' => $_SERVER,
77
            'get' => $_GET,
78
            'env' => $_ENV,
79
            'simple_url' => \Xhgui_Util::simpleUrl($uri),
80
            'request_ts' => $requestTs,
81
            'request_ts_micro' => $requestTsMicro,
82
            'request_date' => date('Y-m-d', $time),
83
        );
84
        return $data;
85
    }
86
}
87