XhguiRuns   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 1
cbo 1
dl 0
loc 90
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A __construct() 0 4 1
A get_run() 0 3 1
B save_run() 0 38 5
A getCanonicalUrl() 0 12 3
1
<?php
2
3
namespace Jns\Bundle\XhprofBundle\Entity;
4
5
use iXHProfRuns;
6
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
class XhguiRuns implements iXHProfRuns, ContainerAwareInterface
10
{
11
    /**
12
     * @var ContainerInterface
13
     */
14
    protected $container;
15
16
    /**
17
     * Sets the Container associated with this Controller.
18
     *
19
     * @param ContainerInterface $container A ContainerInterface instance
20
     */
21
    public function setContainer(ContainerInterface $container = null)
22
    {
23
        $this->container = $container;
24
    }
25
26
    public function __construct($serverName, $uri) {
27
    	$this->serverName = $serverName;
0 ignored issues
show
Bug introduced by
The property serverName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
    	$this->uri = $uri;
0 ignored issues
show
Bug introduced by
The property uri does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function get_run($run_id, $type, &$run_desc) {
35
        throw new \Exception('not implemented');
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function save_run($xhprof_data, $type, $run_id = null) {
42
        $pmu = isset($xhprof_data['main()']['pmu']) ? $xhprof_data['main()']['pmu'] : '';
43
        $wt  = isset($xhprof_data['main()']['wt'])  ? $xhprof_data['main()']['wt']  : '';
44
        $cpu = isset($xhprof_data['main()']['cpu']) ? $xhprof_data['main()']['cpu'] : '';
45
46
        $doctrine = $this->container->get('doctrine');
47
        if (empty($doctrine)) {
48
            throw new \Exception("Trying to save to database, but Doctrine was not set correctly");
49
        }
50
51
        $runId = uniqid();
52
53
        $em = $doctrine->getManager($this->container->getParameter('jns_xhprof.entity_manager'));
54
        $entityClass  = $this->container->getParameter('jns_xhprof.entity_class');
55
56
        $xhprofDetail = new $entityClass();
57
        $xhprofDetail
58
            ->setId($runId)
59
            ->setUrl($this->uri)
60
            ->setCanonicalUrl($this->getCanonicalUrl($this->uri))
61
            ->setServerName($this->serverName)
62
            ->setPerfData(gzcompress(serialize(($xhprof_data))))
63
            ->setCookie(serialize($_COOKIE))
64
            ->setPost(serialize($_POST))
65
            ->setGet(serialize($_GET))
66
            ->setPmu($pmu)
67
            ->setWt($wt)
68
            ->setCpu($cpu)
69
            ->setServerId(getenv('SERVER_NAME'))
70
            ->setAggregateCallsInclude('')
71
            ->setTimestamp(new \DateTime())
72
            ;
73
74
        $em->persist($xhprofDetail);
75
        $em->flush();
76
77
        return $runId;
78
    }
79
80
    /**
81
     * Return the canonical URL for the passed in one.
82
     *
83
     * @param  String $url
84
     * @return String
85
     */
86
    protected function getCanonicalUrl($url)
87
    {
88
        if ($url[0] == '#') {
89
            $url = substr($url, 1, -1);
90
91
            if (substr($url, -1) == '$') {
92
                $url = substr($url, 0, -1);
93
            }
94
        }
95
96
        return $url;
97
    }
98
}
99
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
100